forked from ComputationalPhysics2015-IPM/Python-01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.py
More file actions
43 lines (31 loc) · 737 Bytes
/
fib.py
File metadata and controls
43 lines (31 loc) · 737 Bytes
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
def fib_loop(n):
'''
Returns the nth number in the fibonachi series.
It uses for loop to calculate it.
'''
fib = [1,1]
for i in range(n-2):
x= fib[i+1]+fib[i]
fib.append(x)
return fib[n-1]
def fib_recursion(n):
'''
Returns the nth number in the fibonachi series.
It uses for loop to calculate it.
'''
fib1=1
fib2=1
for i in range(n-2):
fib3 = fib2 + fib1
fib1 = fib2
fib2 = fib3
return fib3
print(fib_loop(8))
print(fib_recursion(13))
def fib_generator(n):
'''
Generator version of fibonacci.
'''
print(fib_loop(8))
print(fib_recursion(13))
print([i for i in fib_generator(14)])