Problem : Even Fibonacci numbers
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.
Answer
before =1
now=0
next =0
sum=0
while now < 4000000:
next = now + before
before = now
now = next
if (now % 2) ==0:
sum= sum + now
print (sum)
Other
p = 1
n = 2
s = 0
while s < 1000000:
s += n
p, n = 2 * n + p, 3 * n + 2 * p
print s
출처 : https://projecteuler.net/problem=2
'Device & Language > Python' 카테고리의 다른 글
python eulerproject 6th : Sum square difference (0) | 2017.08.24 |
---|---|
python eulerproject 5th : Smallest multiple (0) | 2017.08.16 |
python eulerproject 4th : Largest palindrome product (0) | 2017.08.16 |
python eulerproject 3rd : Largest prime factor (0) | 2017.04.06 |
python eulerproject 1st (0) | 2014.12.23 |