본문 바로가기

Algorithm

[Euler Project] Problem 25 using Python

Euler Project 25번 문제는 피보나치 수열 문제이다.

꽤나 큰 수의 피보나치 수(1000자리)가 등장해야 문제가 종료되고, 전체 피보나치 수열이 필요 없으므로, 필요한 정보만 관찰한다.

관찰할 정보는 관찰중인 피보나치 수열의 마지막 두 수와 전체 피보나치 수열의 길이이다.

 

 

"""
The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.

What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
"""

f_b = 1
f_bb = 1
f_len = 2

while(True):
    temp = f_b

    f_b = temp + f_bb
    f_bb = temp

    f_len += 1

    if f_b > 10**999:
        print(f_len)
        break