Python:超出递归深度错误

Python:超出递归深度错误,python,Python,我修改了程序,得到了大素数。现在它工作得很好,除了在打印完所有输出后,我在结尾处得到了大量的红色短语。有人能告诉我为什么吗 输出的结尾是: (100000939.0, 'is prime') (100000963.0, 'is prime') (100000969.0, 'is prime') 错误是 Traceback (most recent call last): File "C:/Users/Marcela/Documents/Prime numbers to 100", line

我修改了程序,得到了大素数。现在它工作得很好,除了在打印完所有输出后,我在结尾处得到了大量的红色短语。有人能告诉我为什么吗

输出的结尾是:

(100000939.0, 'is prime')
(100000963.0, 'is prime')
(100000969.0, 'is prime')
错误是

Traceback (most recent call last):
  File "C:/Users/Marcela/Documents/Prime numbers to 100", line 48, in <module>
    loopfunction()
  File "C:/Users/Marcela/Documents/Prime numbers to 100", line 35, in loopfunction
    loopfunction()
  File "C:/Users/Marcela/Documents/Prime numbers to 100", line 35, in loopfunction
    loopfunction()
  File "C:/Users/Marcela/Documents/Prime numbers to 100", line 35, in loopfunction
    loopfunction()
...(many lines of it, the int ends with:) 
File "C:/Users/Marcela/Documents/Prime numbers to 100", line 13, in loopfunction
    while index <= 200000000.0:
RuntimeError: maximum recursion depth exceeded in cmp
回溯(最近一次呼叫最后一次):
文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第48行,在
loopfunction()
文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第35行,循环函数
loopfunction()
文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第35行,循环函数
loopfunction()
文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第35行,循环函数
loopfunction()
…(很多行,int以:)
文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第13行,循环函数

而index可以通过简单地不递归来解决递归限制问题。您的代码已经具有所需的循环

只需将各种if语句中对
loopfunction()
的调用更改为
break

虽然这就是代码实现功能所需的全部内容,但我注意到一些if语句是不必要的(或者与循环条件冗余,或者根本不可能命中)。您还使用了
while
循环,其中
for uuin range()
循环更有意义。下面是代码的一个大大简化的版本,其中一些内容被重命名以阐明其用途:

from math import sqrt

def primeFinder(start, end):
    prime = True

    for index in range(start, end):
        for checker in range(2, int(sqrt(index))+1):
            if index % checker == 0:
                prime = False
                break

        if prime:
            print("%d is prime" % index)

        prime = True

primetest(100000000, 200000001)

这个版本仍然需要很长时间(可能需要几个小时)。如果您真的想测试一亿个素数值,您可能应该研究更好的算法,例如。

什么是“红色短语”?请向我们展示您的输出。RuntimeError:调用Python对象时超过了最大递归深度,这是您的错误,其他行都是由于这个原因。谢谢--有没有办法避免超过最大递归深度?在我看来,这个脚本似乎是合乎逻辑的。“红色的东西”是:(10000937.0,“是质数”)(10000939.0,“是质数”)(10000963.0,“是质数”)(10000969.0,“是质数”)(最近的调用最后一次):文件“C:/Users/Marcela/Documents/prime numbers To 100”,第48行,在loopfunction()文件“C:/Users/Marcela/Documents/prime numbers To 100”中,第35行,在loopfunction()中(更多相同之处!)。。。文件“C:/Users/Marcela/Documents/Prime numbers to 100”,第13行,在loopfunction中,而索引一个更好的建议是将递归转换为迭代。非常好!!我将这三个实例改为“break”,它像一个符咒一样工作,数字大小不一。再也没有红色短语了。你是个救命恩人,我的朋友,你真是太棒了!您自己的ode更精简、更好,但为了掌握这一点,我仍然想知道为什么我自己的ode不起作用——只需将函数调用更改为break和presto。
from math import sqrt

def primeFinder(start, end):
    prime = True

    for index in range(start, end):
        for checker in range(2, int(sqrt(index))+1):
            if index % checker == 0:
                prime = False
                break

        if prime:
            print("%d is prime" % index)

        prime = True

primetest(100000000, 200000001)