forked from feroda/lessons-python4beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
executable file
·65 lines (49 loc) · 1.36 KB
/
t.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
#!/usr/bin/python2
import sys
import time
MAX = 1050
# 1 1 2 3 5 8 13 21
rv_fib = {
0: 1,
1: 1
}
def fib(x):
if x in rv_fib:
return rv_fib[x]
rv_fib[x-1], rv_fib[x-2] = fib(x-1), fib(x-2)
return rv_fib[x-1] + rv_fib[x-2]
def get_input():
try:
num = int(sys.argv[1])
# num = int(raw_input("\nQuanto vuoi calcolare fibonacci [max {}]? ".format(MAX)))
except Exception:
pass
else:
return num
def do():
print("\n--- Il programma di fibonacci ---")
while True:
num = get_input()
if num and num <= MAX:
break
rv = fib(num)
print("\n--- Il risultato di fib({}) e' {} ---\n".format(num, rv))
choice = 'y' # raw_input("Ti mostro il procedimento? [Y/n] ")
if choice.upper() != "N":
for step in range(num+1):
if step == 0 or step == 1:
print("fib({}) = {}".format(step, fib(step)))
else:
print("fib({}) = fib({}) + fib({}) = {} + {} = {}".format(step, step-2, step-1, fib(step-2), fib(step-1), fib(step)))
# time.sleep(0.3)
def main():
try:
while True:
print("\n## Per uscire premi CTRL+C su Unix")
do()
break
except KeyboardInterrupt:
print("\nSei voluto uscire")
sys.exit(100)
if __name__ == "__main__":
main()