We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 16c401a + ed0d247 commit b6f6ee5Copy full SHA for b6f6ee5
fast_recursive_fibonacci.py
@@ -0,0 +1,21 @@
1
+def fast_rec_fib_helper(n):
2
+ """returns the last two elements of the fibonacci sequence."""
3
+ if n <= 1:
4
+ return (0,1)
5
+ m = n//2
6
+ hprv, hcur = fast_rec_fib_helper(m)
7
+ prev = (hprv ** 2) + (hcur **2)
8
+ curr = hcur * (2 * hprv + hcur)
9
+ next = prev + curr
10
+ if n % 2 == 0:
11
+ return (prev, curr)
12
+ else:
13
+ return (curr, next)
14
+
15
+def fast_rec_fib(n):
16
+ if n==0:
17
+ return 0
18
+ previous, current = fast_rec_fib_helper(n)
19
+ return current
20
21
+print(fast_rec_fib(2))
0 commit comments