Algorithm
[Project_Euler] Even Fibonacci Numbers
seandoesdev
2024. 4. 5. 17:49
Problem
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.
피보나치 수열의 각 항은 바로 앞의 항 두 개를 더한 것이 됩니다. 1과 2로 시작하는 경우 이 수열은 아래와 같습니다.
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
짝수이면서 4백만 이하인 모든 항을 더하면 얼마가 됩니까?
Java Code
Python Code
def fib(n):
sum=0
a, b = 1, 2
while a < n:
print(a)
a, b = b, a+b
if a%2==0:
sum+=a
return sum
print('result='+str(fib(4000000)))