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로 시작하는 10 개의 항은 다음과 같다.
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
값이 400만을 초과하지 않는 피보나치 수열의 항을 고려하여, 짝수 항의 합계를 구하시오.
# 풀이
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
a = 0
b = 1
f = 0
fibo = []
even_fibo = []
while f < 4000000:
f = a + b
fibo.append(f)
a = b
b = f
for i in fibo:
if i % 2 == 0:
even_fibo.append(i)
print(fibo)
print(even_fibo)
print(sum(even_fibo)) # 답 : 4613732
|
cs |
'Python & SQL > Python Problems' 카테고리의 다른 글
| [프로젝트 오일러/파이썬] Largest palindrome product (0) | 2021.04.18 |
|---|---|
| [프로젝트 오일러/파이썬] Largest prime factor (0) | 2021.04.18 |
| [프로젝트 오일러/파이썬] Multiples of 3 and 5 (0) | 2021.04.18 |
| [점프 투 파이썬] 05장 연습 문제 풀이 (0) | 2021.04.18 |
| [점프 투 파이썬] 04장 연습 문제 풀이 (0) | 2021.04.18 |