Coding/Python
[euler] Problem 2 피보나치 수열;;
0w0
2017. 7. 17. 04:33
728x90
반응형
http://euler.synap.co.kr/prob_detail.php?id=2
피보나치 수열의 각 항은 바로 앞의 항 두 개를 더한 것이 됩니다. 1과 2로 시작하는 경우 이 수열은 아래와 같습니다.
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
짝수이면서 4백만 이하인 모든 항을 더하면 얼마가 됩니까?
소스
temp=0
n1=1
n2=2
result=0
while temp<=4000000:
temp=n1+n2
print(temp, n2)
n1=n2
n2=temp
if n1%2==0:
result+=n1
print(result)
실행
728x90
반응형