-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_even_fib.py
More file actions
53 lines (22 loc) · 755 Bytes
/
sum_even_fib.py
File metadata and controls
53 lines (22 loc) · 755 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
43
44
45
46
47
# Fibbonacci Sequence
# this will return the sum of all even items in fibonacci seq
# upto your entered limit
limit = int(input("enter the limit: "))
def fibonacci_seq(limit):
fibblist = [1]
even_list = list()
for i in range(limit):
if fibblist[i] < 4000000:
present_term = fibblist[i]
previous_term = fibblist[i-1]
next_term = previous_term + present_term
fibblist.append(next_term)
# check the value, if it is even , then add that value into even_list
if next_term % 2 == 0:
even_list.append(next_term)
elif fibblist[i] >= 4000000:
print(f"the value reached {fibblist[i]}.")
break
return fibblist, sum(even_list)
fibo_seq, sum_even_fib = fibonacci_seq(limit)
print(sum_even_fib)