简体   繁体   English

试图找到第1000个素数

[英]Trying to find the 1000th prime number

I am trying to write a script in python to find the 1000th prime number. 我正在尝试在python中编写脚本以找到第1000个素数。 I don't understand why this isn't working here. 我不明白为什么这在这里不起作用。 Basically while the mod is less than the square root of the number and still has a remainder, the mod goes up by one. 基本上,当mod小于数字的平方根并且仍然有余数时,mod会增加一。 This should continue until the mod equals the square root of the number. 这应该一直持续到mod等于数字的平方根为止。 Then the check should remain at 0 and the number should be prime. 然后,支票应保持为0,且数字应为质数。 Every time I try to run the script it tells me theres a system error. 每次我尝试运行脚本时,都会告诉我系统错误。

import math
b=2
count=2
next_odd=3
next_prime=1
check = 0

while count<=10:
    while b<float(math.sqrt(next_odd)):
        if next_odd%b>0:
                b+=1
        if next_odd%b == 0:
                check+=1
    if check > 0:
        next_prime=next_odd
        next_odd+=2
        print(next_prime)
        b=2
        count+=1`

I understand what you are trying to do, but unfortunately there were too many things wrong with your program. 我了解您要尝试执行的操作,但是很遗憾,您的程序有太多错误。 Here is a working version. 这是一个工作版本。 I made minimal changes. 我做了最小的改变。 Hopefully you can compare the below version with your own and see where you went wrong. 希望您可以将以下版本与您自己的版本进行比较,看看您出了什么问题。

import math

count=2
next_odd=3
next_prime=1

while count<=1000:
    b=1
    check = 0
    while b<float(math.sqrt(next_odd)):
        b+=1
        if next_odd%b == 0:
            check+=1
    if check == 0:
        next_prime=next_odd
        print(next_prime)
        count+=1
    next_odd+=2

With the above program, 1000th prime can be successfully determined to be 7919. 通过上述程序,可以成功确定第1000个素数为7919。

(first, I assume the tick on the end of your code is a typo in your stack overflow post, not the code itself) (首先,我认为代码末尾的滴答是堆栈溢出后的错字,而不是代码本身)

Consider what happens when next_odd is prime. 考虑当next_odd为质数时发生的情况。 This block: 该块:

while b<float(math.sqrt(next_odd)):
    if next_odd%b>0:
            b+=1
    if next_odd%b == 0:
            check+=1

will increment b up until the square root of next_odd without ever incrementing check . 将递增b直到next_odd平方根,而不再递增check That means that if check > 0: won't pass, and thus count never increments, and you then you just spin around in the while count<=10: , skipping both if blocks because their conditions are false. 这意味着, if check > 0:不会通过,因此count永远不会增加,然后您就在while count<=10:旋转,跳过两个if块,因为它们的条件都是false。

In other words, you don't actually say what to do when next_odd is prime. 换句话说,当next_odd为素数时,您实际上并没有说该怎么办。 This is also an example of why while shouldn't really be used when all you want to do is increment through numbers (which is what you're using it for here). 这也是为什么一个例子while应该在所有你想要做的就是通过数字增量(这是你使用它在这里是什么)没有真正被使用。 Try something like this: 尝试这样的事情:

max_num = 10000 # or whatever
for odd in range(3, max_num, 2):
    factor_count = 0
    for factor in range(2, math.floor(math.sqrt(max_num)) + 1):
        if odd % factor == 0:
            factor_count += 1
    if factor_count == 0:
        print(odd)

A couple points about this code: 关于此代码的几点要点:

  • There's no (non-constant) variables in the global scope. 全局范围内没有(非常数)变量。 That makes it much easier to reason about how the script's state changes over time. 这使得推断脚本状态随时间变化的方式更加容易。
  • The use of for-loops over while-loops guarantees that our script won't get caught in an infinite loop due to an erroneous (or unaccounted for) condition. 在while循环上使用for循环可确保我们的脚本不会由于错误(或无法解释的)情况而陷入无限循环。
  • The use of for-loops means that we don't have to worry about incrementing all of the variables ourselves, which dramatically reduces the amount of state that we have to manage. 使用for循环意味着我们不必担心自己增加所有变量,从而大大减少了我们必须管理的状态量。

Hope that helps! 希望有帮助!

Oh, note that there are much more efficient ways to compute primes also. 哦,请注意,还有许多更有效的方法可以计算素数。 See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 参见https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

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

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