简体   繁体   English

求素数

[英]Finding a prime number

The problem is that you need to find the prime number after the number input, or if the number input is prime, return that. 问题是您需要在输入数字后找到素数,或者如果输入的是素数,则返回该素数。 It works fine. 工作正常。 It's just not working when the input is print(brute_prime(1000)) . 当输入为print(brute_prime(1000))时,它只是不起作用。 It returns 1001 not 1009. The full code is this: 它返回1001而不是1009。完整的代码是这样的:

def brute_prime(n):
    for i in range(2, int(n**(0.5))):
        if n % i == 0:
            n += 1
    else:
        return n

You're not restarting the for i loop when you discover that a number is not prime and go to the next number. 当发现一个数字不是素数并转到下一个数字时,您不会重新启动for i循环。 This has two problems: you don't check whether the next number is a multiple of any of the factors that you checked earlier, and you also don't increase the end of the range to int(n ** 0.5) with the new value of n . 这有两个问题:您不检查下一个数字是否是您先前检查的任何因子的倍数,也没有使用新的将范围的结尾增加到int(n ** 0.5) n值。

def brute_prime(n):
    while true:
        prime = true
        for i in range(2, int(n ** 0.5)+1):
            if n % i == 0:
                prime = false
                break
        if prime:
            return n
        n += 1

break will exit the for loop, and while true: will restart it after n has been incremented. break将退出for循环, while true:将在n增加之后重新启动它。

As Barmar suggests, you need to restart the loop each time you increment n. 正如Barmar建议的那样,每次增加n时,您都需要重新启动循环。 Your range also ends earlier than it should, as a range stops just before the second argument. 您的范围也比其应有的更早结束,因为范围恰好在第二个参数之前停止。

def brute_prime(n):
    while True:
        for i in range(2, int(n**(0.5)) + 1):
            if n % i == 0:
                break
        else:
            return n
        n = n+1

remember 2 is a prime number. 记住2是质数。 again you can just check the division by 2 and skip all the even number division 再次,您可以将其除以2,然后跳过所有偶数除法

 def brute_prime(n):
        while True:
            if n==2:return n
            elif n%2 ==0 or any(n % i==0 for i in range(3, int(n**(0.5)+1),2)):
                n += 1
            else:
                return n

as mention by Chris Martin the wise solution is define a isPrime function separately and use it to get your desire number. 就像克里斯·马丁(Chris Martin)提到的那样,明智的解决方案是分别定义isPrime函数,并使用它来获取所需的数字。

for example like this 例如这样

def isPrime(n):
    #put here your favorite primality test

from itertools import count

def nextPrime(n):
    if isPrime(n):
        return n
    n += 1 if n%2==0 else 2
    for x in count(n,2):
        if isPrime(x):
            return x

if the given number is not prime, with n += 1 if n%2==0 else 2 it move to the next odd number and with count check every odd number from that point forward. 如果给定数字不是素数,则n += 1 if n%2==0 else 2移至下一个奇数,并使用count检查从该点开始的每个奇数。

for isPrime trial division is fine for small numbers, but if you want to use it with bigger numbers I recommend the Miller-Rabin test (deterministic version) or the Baille-PSW test . for isPrime试用版适用于少量数字,但如果您想使用更大的数字,我建议使用Miller-Rabin测试(确定性版本)Baille-PSW测试 You can find a python implementation of both version of the Miller test here: http://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python 您可以在此处找到这两个版本的Miller测试的python实现: http : //rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Python

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

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