Python程序如下:
"""221 may be a prime number."""
import random
def isprime(n,k=128):
if n<2:
return False
for _ in range(k):
a = random.randrange(1,n)
if pow(a,n-1,n)!=1:
return False
return True
参考:https://baike.baidu.com/item/%E8%B4%B9%E9%A9%AC%E5%B0%8F%E5%AE%9A%E7%90%86
Miller-Rabin算法可以把准确度提高4倍,参考:https://blog.csdn.net/s031302306/article/details/51606018
评论