Euler 25项目的非蛮力解决方案 [英] Non Brute Force Solution to Project Euler 25

查看:56
本文介绍了Euler 25项目的非蛮力解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目欧拉问题25 :

斐波那契数列由递归关系定义:

The Fibonacci sequence is defined by the recurrence relation:

F n = F n-1 + F n-2 ,其中F 1 = 1 F 2 =1.因此前12个项 将是F 1 = 1,F 2 = 1,F 3 = 2,F 4 = 3, F 5 = 5,F 6 = 8,F 7 = 13,F 8 = 21,F 9 = 34,F 10 = 55,F 11 = 89,F 12 = 144

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

第12个术语F 12 是第一个包含三个数字的术语.

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

斐波纳契数列中第一个包含1000的项是什么 数字?

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

我在Python中制作了一个蛮力解决方案,但是计算实际解决方案绝对需要永远.谁能建议非暴力解决方案?

I made a brute force solution in Python, but it takes absolutely forever to calculate the actual solution. Can anyone suggest a non brute force solution?

def Fibonacci(NthTerm):
    if NthTerm == 1 or NthTerm == 2:
        return 1 # Challenge defines 1st and 2nd term as == 1
    else:  # recursive definition of Fib term
        return Fibonacci(NthTerm-1) + Fibonacci(NthTerm-2)

FirstTerm = 0 # For scope to include Term in scope of print on line 13
for Term in range(1, 1000): # Arbitrary range
    FibValue = str(Fibonacci(Term)) # Convert integer to string for len()
    if len(FibValue) == 1000:
        FirstTerm = Term
        break # Stop there
    else:
        continue # Go to next number
print "The first term in the\nFibonacci sequence to\ncontain 1000 digits\nis the", FirstTerm, "term."

推荐答案

您可以编写一个fibonacci函数,该函数以线性时间运行并且具有恒定的内存占用,因此不需要列表即可保留它们. 这是一个递归版本(但是,如果n足够大,它将只是 stackoverflow )

You can write a fibonacci function that runs in linear time and with constant memory footprint, you don't need a list to keep them. Here's a recursive version (however, if n is big enough, it will just stackoverflow)

def fib(a, b, n):
    if n == 1:
        return a
    else: 
        return fib(a+b, a, n-1)


print fib(1, 0, 10) # prints 55

此函数仅调用一次(导致大约N调用参数N),而您的解决方案则调用两次(大约2 ^ N调用参数N).

This function calls itself only once (resulting in around N calls for a parameter N), in contrast with your solution which calls itself twice (around 2^N calls for a parameter N).

这是一个永远不会堆栈溢出的版本,它使用循环而不是递归:

Here's a version that won't ever stackoverflow and uses a loop instead of recursion:

def fib(n):
    a = 1
    b = 0
    while n > 1:
        a, b = a+b, a
        n = n - 1
    return a

print fib(100000)

那已经足够快了:

$ time python fibo.py 
3364476487643178326662161200510754331030214846068006390656476...

real    0m0.869s

但是调用fib直到获得足够大的结果并不完美:意甲联赛的第一个数字被多次计算. 您可以计算下一个斐波那契数,并在同一循环中检查其大小:

But calling fib until you get a result big enough isn't perfect: the first numbers of the serie are calculated multiple times. You can calculate the next fibonacci number and check its size in the same loop:

a = 1
b = 0
n = 1
while len(str(a)) != 1000:
    a, b = a+b, a
    n = n + 1
print "%d has 1000 digits, n = %d" % (a, n)

这篇关于Euler 25项目的非蛮力解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆