샤브의 블로그 RSS 태그 관리 글쓰기 방명록
2014-12-27 21:21:01

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