# 用辗转相除求最大公因子
def gcd(a,b):
r=a%b
while(r!=0):
a=b
b=r
r=a%b
return b
# 欧拉函数-暴力循环版
def euler(a):
count=0
for i in range(1,a):
if gcd(a,i)==1:
count+=1
return count
def order(a,n,b):
# 输出b在mod(a)中的阶
# n是mod(a)群的阶
p=1
while(p<=n and (b**p%a!=1)):
p+=1
if p<=n:
return p
else:
return -1
# 求任意数原根
def primitive_root(a):
n=euler(a)
prim=[]
for b in range(2,a):
if order(a,n,b)==n:
prim.append(b)
print(prim)
测试结果:
标签:count,return,gcd,原根,python,数求,a%,def,mod