피보나치 수열
Language/Python

피보나치 수열

뉴비뉴 2019. 7. 17.

문제(https://projecteuler.net/problem=2)

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.

풀이

a = 1
b = 2

for i in range(0,10):
    if i == 9:
        print(a)
        break
    else:
        print(a, end=',')
    c = a+b # 1+2의 값을 c 에 넣는다.
    a = b # 2의 값을 a 에
    b = c # 3의 값을 b 에 넣고 다음 연산 2+3 을 실행하게끔


결과
1,2,3,5,8,13,21,34,55,89

 

댓글

💲 추천 글