Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci. (TODO)

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

memoization

See also https://dlang.org/phobos/std_functional.html#.memoize

(1) memoization
from functools import lru_cache

# 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

@lru_cache
def fibo(n):
    assert n >= 0
    if n < 2:
        return n
    else:
        return fibo(n - 1) + fibo(n - 2)

def main():
    result = fibo(44)  # 701_408_733
    print(result)
import std.stdio;
import std.functional; // memoize

uint fibo(const uint n)
{
    if (n < 2) {
        return n;
    } else {
        return memoize!fibo(n - 1) + memoize!fibo(n - 2);
    }
}

void main()
{
    int result = fibo(44);
    writeln(result); // 701_408_733
}

Notes:

  • In Python, it's enough to use a decorator on a function and we're done.
  • In D, memoize is a template. Use it at each recursive call within the recursive function whose results we want to cache.
  • memoize!fibo(n - 1) means: call fibo(n - 1) and cache its result.
  • It's also possible to limit the size of the cache. For that, read the docs.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 August 20, 21:58