Python/project_euler/problem_02/sol2.py

16 lines
361 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
def fib(n):
2019-02-09 16:59:43 +00:00
"""
Returns a list of all the even terms in the Fibonacci sequence that are less than n.
"""
ls = []
a, b = 0, 1
2018-10-19 12:48:28 +00:00
while b < n:
2019-02-09 16:59:43 +00:00
if b % 2 == 0:
ls.append(b)
2018-10-19 12:48:28 +00:00
a, b = b, a+b
2019-02-09 16:59:43 +00:00
return ls
2018-10-19 12:48:28 +00:00
2019-02-09 16:59:43 +00:00
if __name__ == '__main__':
n = int(input("Enter max number: ").strip())
print(sum(fib(n)))