Automatic commit performed through alias...
This commit is contained in:
39
problems/002_problem.py
Normal file
39
problems/002_problem.py
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
# Problem 2:
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
import decorators
|
||||
|
||||
@decorators.function_timer
|
||||
def main():
|
||||
|
||||
count = 0
|
||||
limit = 4000000
|
||||
n1 = 1
|
||||
n2 = 1
|
||||
sumval = 0
|
||||
|
||||
while count <= limit:
|
||||
|
||||
n3 = n1 + n2
|
||||
|
||||
if n3%2 == 0:
|
||||
sumval += n3
|
||||
|
||||
n1 = n2
|
||||
n2 = n3
|
||||
count = n3
|
||||
|
||||
print("The sum is {}".format(sumval))
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user