Skip to content

Commit 496b002

Browse files
add fast exponentiation
1 parent 0fd1291 commit 496b002

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Fibonacci Sequence/fast-fibonacci.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ def multiply(F, M):
2525

2626
def power(F, n):
2727

28+
if( n == 0 or n == 1):
29+
return;
2830
M = [[1, 1],
29-
[1, 0]]
30-
31-
for i in range(2, n + 1):
31+
[1, 0]];
32+
33+
power(F, n // 2)
34+
multiply(F, F)
35+
36+
if (n % 2 != 0):
3237
multiply(F, M)
33-
38+
39+
3440
n = 11
3541
print(fib(n))

0 commit comments

Comments
 (0)