You are on page 1of 7

Chinese Reminder Theorem

The Chinese Reminder Theorem is an ancient but important calculation algorithm in


modular arith-
metic. The Chinese Remainder Theorem enables one to solve simultaneous equations with respect
to different moduli in considerable generality. Here we supplement the discussion in T&W, §3.4,
pp. 76-78.
The problem
Here is the statement of the problem that the Chinese Remainder Theorem solves.
Theorem (Chinese Remainder Theorem). Let m1 , . . . , mk be integers with gcd(mi , mj ) = 1
whenever i = j. Let m be the product m = m1 m2 · · · mk . Let a1 , . . . , ak be integers. Consider the
system of congruences:
(∗ )
x ≡ a1
x ≡ a2
...
x ≡ ak(mod m1 )(mod m2 )(mod mk ).
Then there exists exactly one x ∈ Zm satisfying this system.
The algorithm
The solution to the system (*) may be obtained by the following algorithm.
Theorem (Chinese Remainder Theorem Algorithm). We may solve the system (*) asfollows.
(1) For each i = 1, . . . , k, let zi = m/mi = m1 m2 . . . mi−1 mi+1 . . . mk .
−1
(2) For each i = 1, . . . , k, let yi = zi (mod mi ). (Note that this is always possible because
gcd(zi , mi ) = 1.)
(3) The solution to the system (*) is x = a1 y1 z1 + · · · + ak yk zk .
Proof. Why does the Chinese Remainder Theorem algorithm work? The notation makes the
proof
surprisingly simple to state. Let’s study x = a1 y1 z1 + · · · + ak yk zk and compute x (mod
m1 )
for example. The same argument will work for x (mod mi ) for i > 1. The key
observa-tion (and a very clever one too) is that zi ≡ 0 (mod m1 ) when i = 1 since m1 divides
zi =m/mi = m1 m2 . . . mi−1 mi+1 . . . mk . Thus when we compute x (mod m1 ), we obtain
x ≡ a1 y1 z1(mod m1 ). But y1 z1 ≡ 1 (mod m1 ) by (2), and we obtain x ≡ a1 (mod m1 ).
Example of the Chinese Remainder Theorem
Use the Chinese Remainder Theorem to find all solutions in Z60 such that
x ≡ 3 mod 4
x ≡ 2 mod 3
x ≡ 4 mod 5.
We solve this in steps.
Step 0 Establish the basic notation. In this problem we have k = 3, a1 = 3, a2 = 2, a3 = 4,
m1 = 4, m2 = 3, m3 = 5, and m = 4 · 3 · 5 = 60.
Step 1 Implement step (1). z1 = m/m1 = 60/4 = 3 · 5 = 15, z2 = 20, and z3 = 12.
Step 2 Implement step (2). We solve zi yi ≡ 1 mod mi , i = 1, 2, 3. In this problem, we need
to solve
15y1 ≡ 1 mod 4
20y2 ≡ 1 mod 3
12y3 ≡ 1 mod 5.
The yi can be computed using the tally table version of the generalized Euclidean algorithm
(cf. Congruence Supplement). For example, in the first equation for y1 , the tally method
automatically solves 15y1 + 4t = 1 for y1 and t, and we find that y1 = 3. Continuing, we
find that y1 = 3, y2 = 2, and y3 = 3.
Step 3 Implement step (3). x ≡ a1 y1 z1 + a2 y2 z2 + a3 y3 z3 (mod 60). Substituting, we obtain
3 · 3 · 15 + 2 · 2 · 20 + 4 · 3 · 12 = 359 which reduces to x ≡ 59 (mod 60).
An application
The text (p. 74) emphasizes the opposite line of thought from the above. Now we wish to solve
the equation x ≡ a (mod m) where m is a multiple of two or more pairwise relatively prime
integers. The Chinese Remainder Theorem Algorithm tells us that the x is precisely the solution
to the modular system
(∗ )
Chinese Reminder Theorem
x ≡ a1
x ≡ a2
...
x ≡ ak(mod m1 )(mod m2 )(mod mk ).

Here the numbers mi come by factoring m = m1 m2 · · · mk where gcd(mi , mj ) = 1


whenever i = j.
Why do this? This is answered in the text (T&W, p. 77). By breaking the problem into si-
multaneous congruences mod each prime factor of m, we can recombine the resulting information
to obtain an answer for each prime factor power of m. The advantage is that it is often easier to
analyze congruences mod primes (or mod prime powers) than to work with composite numbers.
Example. Here is an example. Find a solution to 13x ≡ 1 (mod 70). The two methods of solution
are worthy of careful study.
Answer. We can do this particular example two ways. First notice that 13−1 = 27 (mod 70) by
the usual tally table generalization of the Euclidean algorithm. So the given problem is equivalent
to x ≡ 27 (mod 70).
We can also do it by the Chinese Remainder Theorem. Now 70 = 2 · 5 · 7. First solve
13r1 ≡ 1 mod 2
13r2 ≡ 1 mod 5
13r3 ≡ 1 mod 7,
obtaining r1 = 1, r2 = 2, and r3 = 6. Now solve
x ≡ r1 = 1 (mod 2)
x ≡ r2 = 2 (mod 5)
x ≡ r3 = 6 (mod 7)
by the Chinese Remainder Theorem, obtaining x = 27 ∈ Z70 once more.
#include <stdio.h>
void baca(int A[], int B[], int g)
{
int i,temp;
for(i=0;i<g;i++)
{
printf("\t\t\tx = a mod b :for a<b\n");
printf("\t\t\t***********************\n");
do
{
printf("\t\t\t a = " ); scanf("%d",&A[i]);
printf("\t\t\t b = " ); scanf("%d",&B[i]);
if(A[i]>B[i])
{
printf("\t\t\t******************\n");
printf("\t\t\tif data [a> b] are not in please repeat the process until the
data input b> a\n");
printf("\t\t\t******************\n");
}
else
printf("\t\t\tThe data you input is correct\n");
}while(A[i]>B[i]);
}

}
void tulis(int A[], int B[], int u)
{
int i;
for(i=0;i<u;i++)
{ printf("\t\t\t..............\n");
printf("\t\t\tx = %d mod %d \n",A[i],B[i]);
printf("\t\t\t..............\n");
}
}
void proses(int a[], int b[], int h[], int n)
{
int i,w,j,e,k,temp,r;
int q,we;
w=1;
for(i=0;i<n;i++)
{
w=w*b[i];
}
for(i=0;i<n;i++)
{
j=1;
e=1;
k=w/b[i];
do
{
r=(k*j-1)%b[i];
j++;
}while(r!=0);
j--;
temp=h[i];
h[i]=j;
j=temp;

}
q=0;
for(i=0;i<n;i++)
{
q=q+(w/b[i])*a[i]*h[i];
}
we=q%w;
printf("\t\t\t*****************************\n");
printf("\t\t\tnumbers in the search is= %d\n",we);
printf("\t\t\t%d = %d mod %d\n",q,we,w);
printf("\t\t\t*****************************\n");
}
int gcd(int A[], int B[], int n)
{
int i,sisa,flag,r,s;
i=0;flag=0;
while(i<n && flag==0)
{
r=A[i];s=B[i];
do
{
sisa=s%r;
s=r;
r=sisa;
}while(sisa!=0);
if(s==1)
{
flag=0;
}
else
{
flag=1;
}
i++;
}
return flag;
}
main()
{
int n,j;j=0;
char pilih;
pilih='y';
int a[6]; int b[6]; int h[6];
printf("\t\t\tWELCOME ......................\n");
printf("\t\t\tFARIHUL ROUF : NIM : 10108172586\n");
printf("\t\t\t.......................................\n");
printf("\t\t\tC source at compile it with gcc on GNU Linux\n");
printf("\t\t\t.......................................\n");
printf("..............................\n");
printf("..........................\n");
do
{
printf("\t\thow much data that the input to find solutions : "); scanf("%d",&n);
baca(a,b,n);
tulis(a,b,n);
j=gcd(a,b,n);
if(j==0)
{
proses(a,b,h,n);
}
else
{
printf("\t\t\tgcd is not equal to 1 \n");
printf("\t\t\tNot eligible [mi*nj]=1\n");
printf("\t\t\tThere's no problem-solving solutions\n");
}
printf("select Y / T to continue or come out of the program = "); scanf("%s",&pilih);
}while(pilih=='y' || pilih=='Y');
if(pilih!='y' || pilih!='Y')
{
printf("\t\t\tYou've come out of the program\n");
printf("\t\t\tThanks\n");
}

You might also like