-
Notifications
You must be signed in to change notification settings - Fork 88
/
fibonacci.py
41 lines (33 loc) · 1.16 KB
/
fibonacci.py
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
#coding: utf-8
''' mbinary
#######################################################################
# File : fibonacci.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.github.io
# Github: https://github.com/mbinary
# Created Time: 2019-02-03 19:10
# Description: use matrix and fast pow to calculate big numbr fibonacci value.
#######################################################################
'''
def fib(n):
"""Calculates the nth Fibonacci number"""
mat, p = (1, 1, 1, 0), n-2
if n <= 0: # for negative fib item, use f(n) = f(n+2)-f(n-1) to calculate
mat = (0, 1, 1, -1), 2-n
li = matrix_pow((0, 1, 1, -1), 1-n)
return li[0]+li[1]
def matrix_pow(mat, n):
ans = (1, 0, 0, 1) # element matrix
while n > 0:
if n % 2 == 1:
ans = matrix_mul(ans, mat)
n >>= 1
mat = matrix_mul(mat, mat)
return ans
def matrix_mul(a, b):
'''a,b are four-item tuple, represent matrix [[a[0],a[1]],[a[2],a[3]]]'''
return a[0]*b[0]+a[1]*b[2], a[0]*b[1]+a[1]*b[3], a[2]*b[0]+a[3]*b[2], a[2]*b[1]+a[3]*b[3]
if __name__ == '__main__':
for i in range(-5, 5):
print(i, fib(i))