任意给定两个素数p和q,p!= q,记 N = p * q ,构造Zn*,问:1、是否每个元素都有inverse?是否成群? 2、这个集合有多少元素?
编程如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 24 13:57:29 2018
@author: axa
"""
#随机生成p,q两个不相等的素数
r1 = int(1)
r2 = int(1)
import random
random.seed() #指定 seed。
while(r1 or r2):
# 素数大于 1
r1 = r2 = 1
n = random.randint(1,20) #包括1和100的随机整数。
m = random.randint(1,20) #包括1和100的随机整数。
if n > 1 and m > 1 and n != m:
for i in range(2,m):
if (m % i) == 0:
break
else:
r1 = 0
for i in range(2,n):
if (n % i) == 0:
break
else:
r2 = 0
print ("p为",n)
print("q为",m)
#生成Zn*,判断Zn*有多少个元素
Zn = n * m
print ("N为",Zn)
num = int(0)
list = []
#list.append(1)
for i in range (1,Zn): #选出N以内的与N互素的数
t = int (0)
tempi = i
tempZn = Zn
while(tempZn%tempi):
t = tempi
tempi = tempZn % tempi
tempZn = t
if(tempi==1):
list.append(i) #放在列表list里
num += 1
print("一共有",num,"个元素")
#判断Zn*中的元素是否有逆元
have_inv = int(1)
for i in range(0,num):
#for j in range(0,num):
j = int(0)
while(have_inv):
if ((list[i] * list[j]) % Zn == 1) :
have_inv = 0
break
else:
j += 1
if have_inv == 1:
print("Zn*没有逆元")
else :
print("Zn有逆元")
#判断Zn*是否为群
is_qun = int(0)
for i in range(0,num):
for j in range(i,num):
for t in range(0,num):
if (list[i] * list[j]) % Zn != list[t]:
is_qun = 0
else :
is_qun = 1
break
if is_qun == 0:
print("Zn*不是群")
else :
print("Zn是循环群")
---