Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在python中生成第1000个素数? candidate=1 素数计数器=1 当主计数器0时: 测试=测试+1 如果候选者==测试: 素数计数器=素数计数器+1 打印“第1000个素数为:”+str(候选)_Python 2.7 - Fatal编程技术网

Python 2.7 如何在python中生成第1000个素数? candidate=1 素数计数器=1 当主计数器0时: 测试=测试+1 如果候选者==测试: 素数计数器=素数计数器+1 打印“第1000个素数为:”+str(候选)

Python 2.7 如何在python中生成第1000个素数? candidate=1 素数计数器=1 当主计数器0时: 测试=测试+1 如果候选者==测试: 素数计数器=素数计数器+1 打印“第1000个素数为:”+str(候选),python-2.7,Python 2.7,我正试图用python为麻省理工学院开放式课程的作业生成第1000个素数。我在互联网上找到了这段代码,它很有效,但我并不完全理解它。有人能一步一步地向我解释一下这个代码是如何工作的吗?candidate=1#这是测试素数的数字 candidate = 1 prime_counter = 1 while prime_counter < 1000: test = 2 candidate = candidate + 2 while candidate%test >

我正试图用python为麻省理工学院开放式课程的作业生成第1000个素数。我在互联网上找到了这段代码,它很有效,但我并不完全理解它。有人能一步一步地向我解释一下这个代码是如何工作的吗?

candidate=1#这是测试素数的数字
candidate = 1
prime_counter = 1

while prime_counter < 1000:
    test = 2
    candidate = candidate + 2

    while candidate%test > 0:
        test = test + 1

    if candidate == test:
            prime_counter = prime_counter + 1

print "The 1000th prime is: " + str(candidate)
prime_counter=1#这是我们找到的素数 当素数计数器<1000时:#只查找素数,直到找到第1000个 test=2#这是我们将候选人除以的数字-从2开始,逐步递增 候选=候选+2#启发式-仅测试素数的奇数 当候选者%test>0时:#找到候选者可被整除的最小数字-%找到余数 test=test+1#这并没有精确除以,将测试值增加1 如果候选者==test:#是唯一可以被候选者本身整除的数字(素数的定义)? 素数计数器=素数计数器+1#增加我们找到的素数的数量 打印“第1000个素数为:+str(候选)#打印结果

在任何情况下,这都是非常低效的代码。您应该尝试像ErasThostenes的筛子一样的东西-

当您将
test=2
行向下移动两行,就在
while
循环之前,程序可能会变得更清晰一些

然后,该块通过搜索将
候选
平均分割的最小数来确定
候选
是否为素数。例如,当候选者为25时,
测试将为5。但是,当候选人是29时,
测试也将是29,因为没有更小的数字将其平均分配。(除1之外,这就是
测试从2开始的原因。)

在理解了这一部分之后,您只需要了解,对于找到的每个候选对象,计数器都是递增的。如果该计数器为1000,则表示
候选者
持有第1000个素数

顺便说一下,这个程序把1当作素数,而不是2,这是在作弊。但对于所有大于2的数字,它都有效

candidate = 1 #this is the number to test for prime-ness
prime_counter = 1 #this is the number of primes we have found

while prime_counter < 1000: #only look for prime numbers until we have found the 1000th one
    test = 2 #this is the number we will divide the candidate by - start with 2 and work our way up
    candidate = candidate + 2 #heurisitic - only test odd numbers for prime-ness

    while candidate%test > 0: #find the lowest number that the candidate is divisible by exactly - % finds the remainder
        test = test + 1 #this did not divide exactly, increment the test value by 1

    if candidate == test: #is the only number that is divisble by candidate the candidate itself (definition of prime)?
            prime_counter = prime_counter + 1 #increment the number of primes we have found

print "The 1000th prime is: " + str(candidate) #print the result