def isCarmichaelNumber( x ):
for y in range(2,x):
#check if prime
if math.gcd (x, y) == 1:
if pow(y, x-1, x) != 1:
return False
return True
print(isCarmichaelNumber(1847))
def isCarmichaelNumber(x):
import math
isprime = True
for y in range(2,x):
if math.gcd(x, y) == 1:
if pow(y, x-1, x) != 1:
return False
else:
isprime = False
return not isprime
您没有检查
x
是否为素数。根据定义,卡迈克尔数必须是复合数。对于任何素数x
,pow(y, x-1, x) == 1
对于range(2, x)
中的所有y
,因此将错误地返回True
。1847是素数,这就是为什么你的函数声称它是一个卡迈克尔数。在一种解决方法:
相关问题 更多 >
编程相关推荐