-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonanci.py
74 lines (55 loc) · 1.32 KB
/
fibonanci.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
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
67
68
69
70
71
72
73
74
fib_cache = {}
def fibonacci(n):
if n in fib_cache:
return fib_cache[n]
if n == 0:
value = 0
# First Fibonacci number is 0
elif n == 1 or n == 2:
# print(f'this is how many times 1 or 2 gets called {n}')
value = 1
else:
# print(f'this is the recursion number:{n}')
value = fibonacci(n-1) + fibonacci(n-2)
fib_cache[n] = value
return value
# print(fibonacci(1000))
def fib(n):
a = 1
b = 1
for _ in range(n):
yield a
temp = a
a = b
b = temp + b
fib(5)
x = y = z = "oranage"
print(x)
print(y)
print(z)
# for n in range(1, 50):
# print(f'{n}: {Fibonacci(n)}')
def fibonacci_loop(num):
if num == 0:
return 0
elif num == 1 or num == 2:
return 1
elif num > 2:
a = 1 # variable for (n - 1)
b = 1 # variable for (n - 2)
for _ in range(3, num + 1):
c = a + b # 2
a = b
b = c
# a, b = b, c
print(c)
return c
# (fibonacci_loop(3))
# def fibonacci_recursion(num):
# '''Return a fibonacci sequence value of num'''
# if num == 0:
# return 0
# if num == 1 or num == 2:
# return 1
# # print(num)
# return fibonacci_recursion(num - 2) + fibonacci_recursion(num - 1)