简体   繁体   English

在斐波那契数列中找到第n个素数(更快)

[英]Finding nth number of primes in Fibonacci sequence (Faster)

I'm taking my first programming course, and my assignment has been to list the nth number of prime numbers in the Fibonacci sequence. 我正在上第一门编程课程,我的任务是列出斐波那契数列中第n个素数。 So far I've come up with this: 到目前为止,我已经提出了:

 num = int(input("Enter a number: "))

a = 1
b = 1
c = 0
count = 0
isPrime = True 

while (count < num):
    isPrime = True
    c = a + b

    for i in range(2,c):
        if (c % i == 0):
            isPrime = False
            break

    if (isPrime):
        print (c)
        count = count + 1
    a = b
    b = c

This works, but for any input greater than 10 it takes a really long time, can anyone help me figure out how to make it a bit quicker? 这可行,但是对于任何大于10的输入,它都需要花费很长时间,有人可以帮助我弄清楚如何使其更快一点吗? I assume it's because a,b and c end up becoming really big, but I'm not sure how to fix this. 我认为这是因为a,b和c最终变得非常大,但是我不确定如何解决此问题。

fib = lambda n:reduce(lambda x,n:[x[1],x[0]+x[1]], range(n),[0,1])[0]

shortest and fastest fibonacci numbers one liner script in python. 最短和最快的斐波那契数词在python中是一个衬里脚本。

>>> fib(1000)
43466557686937456435688527675040625802564660517371780402481729089536555417949051
89040387984007925516929592259308032263477520968962323987332247116164299644090653
3187938298969649928516003704476137795166849228875L

found here . 这里找到。

It's easiest to separate generation of fibonacci numbers from testing for primality. 将斐波纳契数的生成与素性测试分开是最容易的。 Here's a Python implementation of the Miller-Rabin primality test: 这是Miller-Rabin素数测试的Python实现:

def isPrime(n, k=5): # miller-rabin
    from random import randint
    if n < 2: return False
    for p in [2,3,5,7,11,13,17,19,23,29]:
        if n % p == 0: return n == p
    s, d = 0, n-1
    while d % 2 == 0:
        s, d = s+1, d/2
    for i in range(k):
        x = pow(randint(2, n-1), d, n)
        if x == 1 or x == n-1: continue
        for r in range(1, s):
            x = (x * x) % n
            if x == 1: return False
            if x == n-1: break
        else: return False
    return True

Then it is easy to generate fibonacci numbers and test them for primality: 然后很容易生成斐波那契数并测试它们的素性:

a, b, f = 1, 1, 2
while True:
    if isPrime(f): print f
    a, b, f = b, f, b+f

It won't take too long to find the 22nd prime fibonacci number: 找到第22个素数斐波纳契数不会太久:

You can see the program in action at http://ideone.com/L1oQgO . 您可以在http://ideone.com/L1oQgO上看到正在运行的程序。 See A005478 or A001605 for more. 有关更多信息,请参见A005478A001605

You definitely should use the following well-known heuristic: 您绝对应该使用以下众所周知的启发式方法:

To check if N is a prime number you only need to divide it by the numbers from 2 to sqrt(N) . 要检查N是否为质数,您只需将其除以2到sqrt(N)的数字即可。

@user448810 implicitly uses it in the Miller-Rabin primality test. @ user448810在Miller-Rabin素数测试中隐式使用它。 But just in case you just want to improve upon your own code. 但是以防万一,您只是想改进自己的代码。

This code does the same as the one line code and is more readable: 此代码与一行代码相同,并且可读性更高:

def fib(n):
    a=1
    b=1
    for i in range(n-2):
        a,b = b,a+b
    return b        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM