-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_wqu.py
81 lines (61 loc) · 1.52 KB
/
fibonacci_wqu.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
75
76
77
78
79
80
81
def first_n_fibonacci(n):
prev_num = 0
curr_num = 1
count = 2
print(prev_num)
print(curr_num)
while count <= n:
next_num = curr_num + prev_num
print(next_num)
prev_num = curr_num
curr_num = next_num
count += 1
def below_x_fibonacci(x):
prev_num = 0
curr_num = 1
if curr_num < x:
print(prev_num)
print(curr_num)
elif prev_num < x:
print(prev_num)
while curr_num + prev_num < x:
next_num = curr_num + prev_num
print(next_num)
prev_num = curr_num
curr_num = next_num
m = 7
print('First %d Fibonacci numbers' % m)
first_n_fibonacci(m)
print()
y = 40
print('Fibonacci numbers below %d' % y)
below_x_fibonacci(y)
def first_n_fibonacci_while(n):
prev_num = 0
curr_num = 1
count = 2
print(prev_num)
print(curr_num)
while count <= n:
next_num = curr_num + prev_num
print(next_num)
prev_num = curr_num
curr_num = next_num
count += 1
def first_n_fibonacci_for(n):
prev_num = 0
curr_num = 1
print(prev_num)
print(curr_num)
for count in range(2, n + 1):
next_num = curr_num + prev_num
print(next_num)
prev_num = curr_num
curr_num = next_num
def fibonacci_recursive(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)