【发布时间】:2019-11-19 22:49:13
【问题描述】:
我正在尝试列出所有小于 10000 的 Carmichael 数字,但是,我认为 print_carmichael 函数存在问题。出于某种原因,当is_carmichael 为真时,它不会打印所有n 值。
def is_carmichael(n):
b = 2
while b<n:
if (gcd(b, n) == 1):
if (pow(b, n - 1, n) != 1):
return 0
b = b + 1
return 1
def print_carmichael(max):
for n in range(2, max):
if is_carmichael(n):
print(n)
return 0
【问题讨论】:
-
它为我打印了很多数字(实际上有点太多,包括非卡迈克尔数字)。您希望看到什么?
-
为我调用
print_carmichael(1000)会导致达到最大递归深度。 -
是的,这就是问题所在。我只想输出 carmichael 数字。
标签: python number-theory