-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibo.py
executable file
·53 lines (43 loc) · 1.1 KB
/
fibo.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
# $Id: fibo.py, 6a8319e303c1 makhtar $
# Module example Fibonacci numbers
__name__ = 'Test fibonacci demoing modularisation'
def fib(n):
''' Generate Fibonacci series up to n '''
print(f"Calculating Fibonacci of {n}")
a, b = 0, 1
while b < n:
print(b, end=' ')
a, b = b, a + b
print()
def fib2(n):
''' Generate Fibonacci array up to n '''
print(f"Fibonnaci array of {n}")
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a + b
return result
def recurseFibo(n):
''' Nth Fibonacci number '''
if n <= 0:
return 0
elif n == 1:
return 1
return recurseFibo(n-1) + recurseFibo(n-2)
def memoizationFibo(n):
'''Use a memo table to pre-compute'''
pass
# make the file usable as a script as well as an importable module
#if __name__ == "__main__":
import sys
n = 0
if len(sys.argv) <= 1:
n = int(input("Input a number to determine its Fibonacci serie: "))
fib(n)
else:
n = int(sys.argv[1])
serie = fib2(n)
print(serie)
print(f"{n}th Fibonacci number...")
print(recurseFibo(n))