前提条件

不解释,数学家的结晶

如果p为素数,在 1~p-1之中,只有1和p-1的平方mod p等于1
证明如下

-1 mod p 可以看作是 p-1 mod p
python代码
`def tobinary(a):
d = []
c = a
while(c!=0):
b = c % 2
c = int(c/2)
d.append(b)
return d``
def ml(n):
for i in range(5): #随机五次
f = tobinary(n - 1) #n-1转化为二进制
c = 0
d = 1
a = random.randint(2,n//2)
while(c!=n-1):
c = 2*c
x = d
d = (d*d)%n
if d == 1: #二次探测定理
if x != 1 and x != n - 1:
return False
g = f.pop()
if(g ==1):
c=c+1
d=(d*a)%n
if d!=1: #费尔玛定理
return False
return True
这篇博客探讨了素数p的一个特性:在1到p-1的范围内,除了1和p-1之外,没有其他数的平方模p等于1。通过二次探测定理和费尔玛定理,作者提供了Python代码来随机验证这个数学结论。
388

被折叠的 条评论
为什么被折叠?



