-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenFiboNum.py
More file actions
87 lines (72 loc) · 2.32 KB
/
Copy pathEvenFiboNum.py
File metadata and controls
87 lines (72 loc) · 2.32 KB
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
82
83
84
85
86
#Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
#PROVA 1 ORRENDA NO MODULARIZZAZIONE, FUNZIONA MA CHE CASINO
#POCO UTILE MA TENIAMOLO PER RICORDARE COME NON SI FA A SCRIVERE UN CODICE
'''
def main():
n=int(input("Inserisci il numero massimo"))
result=Fibonacci(n)
print(result)
def Fibonacci(maxnum):
oldervalue=0
firstnumber=1
secondnumber=0
index=0
fiboNumbers={}
for index in range(maxnum+1):
secondnumber=firstnumber+oldervalue
oldervalue=firstnumber
firstnumber=secondnumber
print(secondnumber)
fiboNumbers[index]=secondnumber
return fiboNumbers
main()
'''
#FIBONACCI VERSIONE 2
def main():
#Greetings
print("Welcome to Fibonacci's numbers analyzer")
print("Let's find even Fibonacci's numbers")
maxnum=int(input("Insert how many Fibonacci's numbers you desidere to analize "))
#Memoized version - Terza soluzione
dim=maxnum+1
memo=[]
{memo.append(0) for i in range(dim)}
#End of configuration for memoized version
#Main loop that prints the values
for i in range(maxnum+1):
if i!=0:
#value=fib(i) Seconda soluzione
value=MemoFib(i,memo) #Terza soluzione
if even(value)!=None:
print(even(value))
#End final comment
print("This is your list of even Fibonacci's numbers analyzing %d Fibonacci's numbers" % maxnum)
#Funzione che stabilisce se un numero è pari o dispari
def even(value):
if value%2 == 0:
return value
#Else skip (is not necessary to print the numbers that are not odd
else:
return print("This is not an odd number %d" %value)
#Seconda soluzione
def fib(num):
if num==1:
return 1
a,b = 1,1
for i in range(num):
a,b = b, a+b
return a
#Memoized version - Terza soluzione
def MemoFib(n,memo):
if memo[n]!=0:
return memo[n]
if n==1 or n==2:
result=1
else:
result=MemoFib(n-1,memo) + MemoFib(n-2,memo)
memo.insert(n,result)
return result
#Main seconda e terza versione
main()