def fibR(n): if n==0: return 0 elif n==1: return 1 else: return fibR(n-1) + fibR(n-2) def fibI(n): if n==0: return 0 elif n==1: return 1 else: previousE = 0 nextE = 1 for i in range(n-1): s = previousE + nextE previousE = nextE nextE = s return s def fibRM(n, memo): if n in memo: return memo[n] else: r = fibRM(n-1, memo) + fibRM(n-2, memo) memo[n] = r return r #r = fibR(37) #print(r) r = fibI(670) print(r) memo = {0: 0, 1: 1} r = fibRM(670, memo) print(r) print(memo) ''' czas: 39 -> 20 40 -> 30 41 -> 45 42 -> 70 43 -> 110 44 -> 170 45 -> 250 46 -> 380 47 -> 4 -> 9 5 -> 15 6 -> 25 7 -> 25 + 13 -> 38 fib(4)--+ | +--------------------------------------+ | | fib(3)--+ fib(2)--+ | | +-------------------------+ +---------+ | | | | fib(2)--+ fib(1) fib(1) fib(0) | +------+ | | fib(1) fib(0) fib(5): 9 5 1 +/- 15 fib(6): 15 + 9 + 1 '''