Created
March 18, 2020 10:40
-
-
Save xettri/6cf6fd1a9964a7229bb0444a1030e37d to your computer and use it in GitHub Desktop.
One of fastest way for Fibonacci series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r = 1000 | |
m = [] | |
def fibo(n): | |
if len(m) == 0: | |
m[:r] = [0] * r; | |
if n == 1 or n == 2: | |
m[n] = n | |
return n | |
else: | |
a1 = m[n - 1] | |
a2 = m[n - 2] | |
if m[n - 1] == 0: | |
a1 = fibo(n - 1) | |
m[n - 1] = a1 | |
if m[n - 2] == 0: | |
a2 = fibo(n - 2) | |
m[n - 2] = a2 | |
return a1 + a2 | |
for i in range(1,r): | |
print(fibo(i)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r = 1000 | |
m = {} | |
def fibo(n): | |
if n == 1 or n == 2: | |
m[str(n)] = n | |
return n | |
else: | |
if str(n - 1) in m: | |
a1 = m[str(n - 1)] | |
else: | |
a1 = fibo(n - 1) | |
m[str(n - 1)] = a1 | |
if (str(n - 2) in m): | |
a2 = m[str(n - 2)] | |
else: | |
a2 = fibo(n - 2) | |
m[str(n - 2)] = a2 | |
return a1 + a2 | |
for i in range(1,r): | |
print(fibo(i)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment