def fib(n): memo = {0:1, 1:1} def recursiveFib(n): if n not in memo: memo[n] = recursiveFib(n-1)+recursiveFib(n-2) return memo[n] return recursiveFib(n)
def fib(x):f1, f2 = 0, 1for x in range(x):f1, f2 = f2, (f1 + f2)return f2- def fib(n):
- memo = {0:1, 1:1}
- def recursiveFib(n):
- if n not in memo:
- memo[n] = recursiveFib(n-1)+recursiveFib(n-2)
- return memo[n]
- return recursiveFib(n)
test.assert_equals(1, fib(0)) test.assert_equals(1, fib(1)) test.assert_equals(2, fib(2)) test.assert_equals(3, fib(3)) test.assert_equals(5, fib(4)) test.assert_equals(8, fib(5)) a = 0 b = 1 for i in range(999): test.assert_equals(b, fib(i)) tmp = a a = b b = tmp + b
- test.assert_equals(1, fib(0))
- test.assert_equals(1, fib(1))
- test.assert_equals(2, fib(2))
- test.assert_equals(3, fib(3))
- test.assert_equals(5, fib(4))
- test.assert_equals(8, fib(5))
- a = 0
- b = 1
for i in range(1000):- for i in range(999):
- test.assert_equals(b, fib(i))
- tmp = a
- a = b
- b = tmp + b