Clean-up
This commit is contained in:
41
src/Python/Problems001-050/Problem002.py
Normal file
41
src/Python/Problems001-050/Problem002.py
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Created on 14 Mar 2017
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for problem 2 of Project Euler
|
||||
https://projecteuler.net/problem=2
|
||||
"""
|
||||
|
||||
from utils import timeit
|
||||
|
||||
|
||||
@timeit("Problem 2")
|
||||
def compute():
|
||||
"""
|
||||
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, ...
|
||||
|
||||
Find the sum of all the even-valued terms in the sequence which do not
|
||||
exceed four million.
|
||||
"""
|
||||
ans = 0
|
||||
limit = 4_000_000
|
||||
x, y = 1, 1
|
||||
z = x + y # Because every third Fibonacci number is even
|
||||
while z <= limit:
|
||||
ans += z
|
||||
x = y + z
|
||||
y = z + x
|
||||
z = x + y
|
||||
|
||||
return ans
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for problem 2: {compute()}")
|
||||
Reference in New Issue
Block a user