`

Project Euler 第25题

F# 
阅读更多
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


The 12th term, F12, is the first term to contain three digits.

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

第一个长度为1000的斐波纳契序列的位置为?

分享到:
评论
1 楼 lampeter123 2009-08-13  
def f(n):
    a, b = 1, 1
    if n==1 or n==2:
        return 1
    else:  
        for i in xrange(n-2):
            a, b = b, a + b
        return b

if __name__ == '__main__':
    i=1
    while True:
        fstring= str(f(i))
        if len(fstring)>=1000:
            print i
            break
        i=i+1

相关推荐

Global site tag (gtag.js) - Google Analytics