-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheven_fibonacci.py
More file actions
53 lines (24 loc) · 893 Bytes
/
even_fibonacci.py
File metadata and controls
53 lines (24 loc) · 893 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):
# we define fibonacci list with first value
fibblist = [1]
# this list will store all the even values in the fibonacci seq
even_list = list()
for i in range(limit):
present_term = fibblist[i]
# for the first term i-1 = 0-1 = -1 &
# the fibblist[-1] = fibblist[1]
# because for first term the list has only one item
previous_term = fibblist[i-1]
next_term = previous_term + present_term
new_term = next_term
fibblist.append(new_term)
# check the value, if it is even , then add that value into even_list
if fibblist[i] % 2 == 0:
even_list.append(new_term)
return fibblist, sum(even_list)
fibo_seq, sum_even_fib = fibonacci_seq(limit)
print(sum_even_fib)