@Lin--
2018-12-23T14:12:37.000000Z
字数 2492
阅读 255
计算机数学
Using CRT to solve:
x ≡ 8 (mod 11)
x ≡ 3 (mod 19)
解:令
记
由拓展欧几里得算法解得
故
解得
Using CRT to solve the system of congruence:
x ≡ 1 (mod 5)
x ≡ 2 (mod 7)
x ≡ 3 (mod 9)
x ≡ 4 (mod 11)
解:由题得:
令:
所以
Write a program(C or Python) to solve CRT
#algorithm of egcd
def egcd(a,b):
r0,r1,s0,s1=1,0,0,1
while b:
q,a,b=a//b,b,a%b
r0,r1=r1,r0-q*r1
s0,s1=s1,s0-q*s1
return r0
#algorithm of CRT
def CRT(a,m):
M,x=1,0
b=list()
b_=list()
for n in range(len(m)):
M=M*m[n];
for i in range(len(m)):
b.append(M//m[i])
'''
compute b_
because b_[i]*b[i]=1(mod m[i])
so b[i]*b_[i]-1=c1*m[i]
than b[i]*b_[i]+c2*m[i]=1
use egcd to compute the result
'''
b_.append(egcd(b[i],m[i]))
'''
when the result is a negative number
let it become a positive number which is a Equivalence class about b_[i]
'''
if b_[i]<0:
b_[i]=b_[i]%m[i]
x=(a[i]*b[i]*b_[i]+x)%M
return x
#test the algorithm
l0=[1,2,3,4]
l1=[5,7,9,11]
print(CRT(l0,l1))
Complete the proof that it is an isomorphism from
解:
①定义f是从
②
设所有
因为
故
且
得
满足单射条件
任取
由于
故存在
有
故
即
总能找到
故满足满射条件
即是一个双射函数
③设
#judge prime number
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
#generate an array contain numbers from 1 to a-1 which is coprime to a
def array_prime(a):
l=[]
for i in range(a):
if gcd(i,a)==1:
l.append(i)
return l
def f(p,q):
n,i=p*q,0
l2=[]
array_prime(n)
for j in array_prime(n):
l1=[j,(j%p,j%q)]
l2.append(l1)
return l2
print(f(5,7))
result
[[1, (1, 1)], [2, (2, 2)], [3, (3, 3)], [4, (4, 4)], [6, (1, 6)], [8, (3, 1)], [9, (4, 2)], [11, (1, 4)], [12, (2, 5)], [13, (3, 6)], [16, (1, 2)], [17, (2, 3)], [18, (3, 4)], [19, (4, 5)], [22, (2, 1)], [23, (3, 2)], [24, (4, 3)], [26, (1, 5)], [27, (2, 6)], [29, (4, 1)], [31, (1, 3)], [32, (2, 4)], [33, (3, 5)], [34, (4, 6)]]