Python: 查找循环组的所有生成器
Python: finding all generators for a cyclic group
取阶为$n$的循环群$\mathbb{Z}_n$。元素是:
$$\mathbb{Z}_n = {1,2,...,n-1}$$.
对于每个元素,让我们称它们为a,你测试是否
$$a^x\pmod n$$
给了我们 $\mathbb{Z}_n$ 中的所有数字; x 在这里是从 1 到 n-1 的所有数字。如果元素确实生成了我们整个组,它就是一个生成器。
我需要一个程序来获取组的顺序并返回所有生成器。这是我尝试过的:
import math
active = True
def test(a,b):
a.sort()
b.sort()
return a == b
while active:
order = input("Order of the cyclic group: ")
print
group = []
for i in range(order-1):
group.append(i+1)
res = []
for x in group:
foo = []
foo.append(x)
for y in group:
foo.append((x**y) % order)
if(test(group,foo)):
res.append(res,foo,axis=0)
print res
遗憾的是,它返回一个空列表。
您可以进行的一项更改是使用集合而不是列表来存储结果,这样可以更轻松地进行比较。
def generators(n):
s = set(range(1, n))
results = []
for a in s:
g = set()
for x in s:
g.add((a**x) % n)
if g == s:
results.append(a)
return results
for i in range(100):
gens = generators(i)
if gens:
print(f"Z_{i} has generators {gens}")
版画
Z_2 has generators [1]
Z_3 has generators [2]
Z_5 has generators [2, 3]
Z_7 has generators [3, 5]
Z_11 has generators [2, 6, 7, 8]
Z_13 has generators [2, 6, 7, 11]
Z_17 has generators [3, 5, 6, 7, 10, 11, 12, 14]
Z_19 has generators [2, 3, 10, 13, 14, 15]
Z_23 has generators [5, 7, 10, 11, 14, 15, 17, 19, 20, 21]
Z_29 has generators [2, 3, 8, 10, 11, 14, 15, 18, 19, 21, 26, 27]
Z_31 has generators [3, 11, 12, 13, 17, 21, 22, 24]
Z_37 has generators [2, 5, 13, 15, 17, 18, 19, 20, 22, 24, 32, 35]
Z_41 has generators [6, 7, 11, 12, 13, 15, 17, 19, 22, 24, 26, 28, 29, 30, 34, 35]
Z_43 has generators [3, 5, 12, 18, 19, 20, 26, 28, 29, 30, 33, 34]
Z_47 has generators [5, 10, 11, 13, 15, 19, 20, 22, 23, 26, 29, 30, 31, 33, 35, 38, 39, 40, 41, 43, 44, 45]
Z_53 has generators [2, 3, 5, 8, 12, 14, 18, 19, 20, 21, 22, 26, 27, 31, 32, 33, 34, 35, 39, 41, 45, 48, 50, 51]
Z_59 has generators [2, 6, 8, 10, 11, 13, 14, 18, 23, 24, 30, 31, 32, 33, 34, 37, 38, 39, 40, 42, 43, 44, 47, 50, 52, 54, 55, 56]
Z_61 has generators [2, 6, 7, 10, 17, 18, 26, 30, 31, 35, 43, 44, 51, 54, 55, 59]
Z_67 has generators [2, 7, 11, 12, 13, 18, 20, 28, 31, 32, 34, 41, 44, 46, 48, 50, 51, 57, 61, 63]
Z_71 has generators [7, 11, 13, 21, 22, 28, 31, 33, 35, 42, 44, 47, 52, 53, 55, 56, 59, 61, 62, 63, 65, 67, 68, 69]
Z_73 has generators [5, 11, 13, 14, 15, 20, 26, 28, 29, 31, 33, 34, 39, 40, 42, 44, 45, 47, 53, 58, 59, 60, 62, 68]
Z_79 has generators [3, 6, 7, 28, 29, 30, 34, 35, 37, 39, 43, 47, 48, 53, 54, 59, 60, 63, 66, 68, 70, 74, 75, 77]
Z_83 has generators [2, 5, 6, 8, 13, 14, 15, 18, 19, 20, 22, 24, 32, 34, 35, 39, 42, 43, 45, 46, 47, 50, 52, 53, 54, 55, 56, 57, 58, 60, 62, 66, 67, 71, 72, 73, 74, 76, 79, 80]
Z_89 has generators [3, 6, 7, 13, 14, 15, 19, 23, 24, 26, 27, 28, 29, 30, 31, 33, 35, 38, 41, 43, 46, 48, 51, 54, 56, 58, 59, 60, 61, 62, 63, 65, 66, 70, 74, 75, 76, 82, 83, 86]
Z_97 has generators [5, 7, 10, 13, 14, 15, 17, 21, 23, 26, 29, 37, 38, 39, 40, 41, 56, 57, 58, 59, 60, 68, 71, 74, 76, 80, 82, 83, 84, 87, 90, 92]
def gen(a,b):
s = set(range(0,a))
g = set()
for i in s:
g.add((i*b)%a)
return g
a = int(input()) #order of Z, e.g Z4, Z5, etc...
s = set(range(0,a))
for i in s:
if(gen(a,i) == s):
print(i)
你,可以试试这个。它会起作用。
取阶为$n$的循环群$\mathbb{Z}_n$。元素是:
$$\mathbb{Z}_n = {1,2,...,n-1}$$.
对于每个元素,让我们称它们为a,你测试是否
$$a^x\pmod n$$
给了我们 $\mathbb{Z}_n$ 中的所有数字; x 在这里是从 1 到 n-1 的所有数字。如果元素确实生成了我们整个组,它就是一个生成器。
我需要一个程序来获取组的顺序并返回所有生成器。这是我尝试过的:
import math
active = True
def test(a,b):
a.sort()
b.sort()
return a == b
while active:
order = input("Order of the cyclic group: ")
print
group = []
for i in range(order-1):
group.append(i+1)
res = []
for x in group:
foo = []
foo.append(x)
for y in group:
foo.append((x**y) % order)
if(test(group,foo)):
res.append(res,foo,axis=0)
print res
遗憾的是,它返回一个空列表。
您可以进行的一项更改是使用集合而不是列表来存储结果,这样可以更轻松地进行比较。
def generators(n):
s = set(range(1, n))
results = []
for a in s:
g = set()
for x in s:
g.add((a**x) % n)
if g == s:
results.append(a)
return results
for i in range(100):
gens = generators(i)
if gens:
print(f"Z_{i} has generators {gens}")
版画
Z_2 has generators [1]
Z_3 has generators [2]
Z_5 has generators [2, 3]
Z_7 has generators [3, 5]
Z_11 has generators [2, 6, 7, 8]
Z_13 has generators [2, 6, 7, 11]
Z_17 has generators [3, 5, 6, 7, 10, 11, 12, 14]
Z_19 has generators [2, 3, 10, 13, 14, 15]
Z_23 has generators [5, 7, 10, 11, 14, 15, 17, 19, 20, 21]
Z_29 has generators [2, 3, 8, 10, 11, 14, 15, 18, 19, 21, 26, 27]
Z_31 has generators [3, 11, 12, 13, 17, 21, 22, 24]
Z_37 has generators [2, 5, 13, 15, 17, 18, 19, 20, 22, 24, 32, 35]
Z_41 has generators [6, 7, 11, 12, 13, 15, 17, 19, 22, 24, 26, 28, 29, 30, 34, 35]
Z_43 has generators [3, 5, 12, 18, 19, 20, 26, 28, 29, 30, 33, 34]
Z_47 has generators [5, 10, 11, 13, 15, 19, 20, 22, 23, 26, 29, 30, 31, 33, 35, 38, 39, 40, 41, 43, 44, 45]
Z_53 has generators [2, 3, 5, 8, 12, 14, 18, 19, 20, 21, 22, 26, 27, 31, 32, 33, 34, 35, 39, 41, 45, 48, 50, 51]
Z_59 has generators [2, 6, 8, 10, 11, 13, 14, 18, 23, 24, 30, 31, 32, 33, 34, 37, 38, 39, 40, 42, 43, 44, 47, 50, 52, 54, 55, 56]
Z_61 has generators [2, 6, 7, 10, 17, 18, 26, 30, 31, 35, 43, 44, 51, 54, 55, 59]
Z_67 has generators [2, 7, 11, 12, 13, 18, 20, 28, 31, 32, 34, 41, 44, 46, 48, 50, 51, 57, 61, 63]
Z_71 has generators [7, 11, 13, 21, 22, 28, 31, 33, 35, 42, 44, 47, 52, 53, 55, 56, 59, 61, 62, 63, 65, 67, 68, 69]
Z_73 has generators [5, 11, 13, 14, 15, 20, 26, 28, 29, 31, 33, 34, 39, 40, 42, 44, 45, 47, 53, 58, 59, 60, 62, 68]
Z_79 has generators [3, 6, 7, 28, 29, 30, 34, 35, 37, 39, 43, 47, 48, 53, 54, 59, 60, 63, 66, 68, 70, 74, 75, 77]
Z_83 has generators [2, 5, 6, 8, 13, 14, 15, 18, 19, 20, 22, 24, 32, 34, 35, 39, 42, 43, 45, 46, 47, 50, 52, 53, 54, 55, 56, 57, 58, 60, 62, 66, 67, 71, 72, 73, 74, 76, 79, 80]
Z_89 has generators [3, 6, 7, 13, 14, 15, 19, 23, 24, 26, 27, 28, 29, 30, 31, 33, 35, 38, 41, 43, 46, 48, 51, 54, 56, 58, 59, 60, 61, 62, 63, 65, 66, 70, 74, 75, 76, 82, 83, 86]
Z_97 has generators [5, 7, 10, 13, 14, 15, 17, 21, 23, 26, 29, 37, 38, 39, 40, 41, 56, 57, 58, 59, 60, 68, 71, 74, 76, 80, 82, 83, 84, 87, 90, 92]
def gen(a,b):
s = set(range(0,a))
g = set()
for i in s:
g.add((i*b)%a)
return g
a = int(input()) #order of Z, e.g Z4, Z5, etc...
s = set(range(0,a))
for i in s:
if(gen(a,i) == s):
print(i)
你,可以试试这个。它会起作用。