Programming/Project Euler - python
Project Euler 2
_Sudal
2019. 1. 30. 08:03
Question:
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백만 이하인 모든 항을 더하면 얼마가 됩니까?
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 | Fibonacci = [1,2] even_sum = 2 for i in range(2,int(4e4)): Fibonacci.append(Fibonacci[i - 2] + Fibonacci[i - 1]) if Fibonacci[i] > int(4e6): break if Fibonacci[i] % 2 == 0: even_sum += Fibonacci[i] print(even_sum) | cs |
피보나치 수열의 첫 두항을 만들고,
수열의 특정항이 4백만 초과 일 때 break.