给定奇整数n>=3和安全参数t
- 随机选取整数b,(b,n)=1,2<=b<=n-2
- 计算r=b的n-1次方(mod n)
- 如果r不等于1,则n是合数
- 上述过程重复t次
from random import random
#from fractions import gcd
def solution(a,b):
#若b>a,则交换两个数的值
if(b>a):
t=a
a=b
b=t
r = b #初始化r
while(r!=0):
r = a%b #r为a/b的余数
a = b
b = r
return a
#得到最后的a为(a,b)
#def solution(n, m):
#return gcd(n, m) == 1
n = int(input("请输入需要检测的整数n:"))
t = int(input("安全参数t:"))
if n<3 and t<0:
print("请输入大于等于3的整数n,大于零的整数t")
exit()
for i in range(t):
b=int(random()*(n-2))
if(2<=b and b<=n-2 and solution(b,n)==1):
r=(b**(n-1))%n
if r!=1:
print("n是合数")
else:
print("不是合数")