We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5dad709 commit 0fd1291Copy full SHA for 0fd1291
Fibonacci Sequence/fast-fibonacci.py
@@ -0,0 +1,35 @@
1
+def fib(n):
2
+ F = [[1, 1],
3
+ [1, 0]]
4
+ if (n == 0):
5
+ return 0
6
+ power(F, n - 1)
7
+
8
+ return F[0][0]
9
10
+def multiply(F, M):
11
12
+ x = (F[0][0] * M[0][0] +
13
+ F[0][1] * M[1][0])
14
+ y = (F[0][0] * M[0][1] +
15
+ F[0][1] * M[1][1])
16
+ z = (F[1][0] * M[0][0] +
17
+ F[1][1] * M[1][0])
18
+ w = (F[1][0] * M[0][1] +
19
+ F[1][1] * M[1][1])
20
21
+ F[0][0] = x
22
+ F[0][1] = y
23
+ F[1][0] = z
24
+ F[1][1] = w
25
26
+def power(F, n):
27
28
+ M = [[1, 1],
29
30
31
+ for i in range(2, n + 1):
32
+ multiply(F, M)
33
34
+n = 11
35
+print(fib(n))
0 commit comments