-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN-th Fibonacci Number.py
More file actions
66 lines (48 loc) · 1.37 KB
/
N-th Fibonacci Number.py
File metadata and controls
66 lines (48 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def fibonacciNumber(n):
# Write your code here.
a = 0
b = 1
if n==1:
print(a)
else:
print(a)
print(b)
for i in range(2,n):
c = a + b
a = b
b = c
print(c)
choice = int(input())
fibonacciNumber(choice)
'''
Time Complexity: O(log(N)).
Space Complexity: O(log(N)).
Where 'N' is the given integer.
def multiply(a, b):
mod = int(1e9 + 7)
c = [[0 for i in range(2)] for j in range(2)]
for i in range(2):
for j in range(2):
for k in range(2):
c[i][k] = (c[i][k] + a[i][j] * b[j][k]) % mod
return c
# Function to calclate n'th power of a matrix.
def matrix_power(coef, n):
# To store the resultant matrix.
res = [[1, 0], [0, 1]]
# While loop till n > 0.
while n > 0:
# If n is odd, multiply res with coef.
if (n % 2 != 0):
res = multiply(res, coef)
# Multiply coef with coef and update n to n//2.
coef = multiply(coef, coef)
n //= 2
return res
def fibonacciNumber(n):
coef = [[0, 1], [1, 1]]
# Calculating the (n-1)th power of the coef matrix.
res = matrix_power(coef, n - 1)
# Return the bottom right element of the resultant matrix.
return res[1][1]
'''