Monday, 30 July 2012

Chinese Remainder Theorem (CRT)


CRT is one of the most handy tools that one can use to crack some complex remainder problems. Let's understand how we can use it.

CRT states that if N is divided by coprimes ‘a’ and ‘b’ and that:
Rem[N/a] = x
Rem[N/b] = y
Then we can arrive at a solution for Rem[N/ab]

We will take 2 examples for understanding this concept. The first example provides an introduction to the second one, which is the kind where we would regularly need to apply CRT.

Example Problem 1: 
Find the lowest integer N which when divided by 5 leaves a remainder of 1, when divided by 7 leaves remainder 4, and when divided by 9 leaves remainder 2.

Soln:
Rem [N/5] = 1
Rem [N/7] = 4
Rem [N/9] = 2

Putting it mathematically, we are looking for N of the following form:
N = 5x + 1 = 7y + 4 = 9z + 2 
Above mathematical representation is the essence of CRT.

By solving (which is much easier by plugging in options whilst in the exam),
we get N = 11 for (x,y,z) = (2,1,1)

  • There are greater values of N possible which would yield similar remainders when divided by 5, 7 and 9. 
  • These can be found out by adding multiples of LCM(5,7,9) = 315 to 11. For example 11 + 315 = 326 is the next such number. 
  • This fact is utilized more extensively in the next example problem, where we are given one such large number to start with but are asked (in a manner) to find out the smallest possible one.

Example Problem 2: 
Find the remainder when 3120 is divided by 105

Soln:
105 = 3 * 5 * 7

If we can find the remainders obtained when 3120 is divided by 3, 5 and 7 respectively, we can apply CRT to find the overall remainder as below:

Rem [3120/3] = 0
Rem [3120/5] = Rem[960/5] = Rem[-160/5] = 1
Rem [3120/7] = Rem[960/7] = Rem[260/7] = Rem[820/7] = 1
  • R = 3x = 5y + 1 = 7z + 1
  • It is important to note that R = 3120, for a particular set of (x,y,z). However, this set does not yield the smallest value for R. As we observed earlier, larger values for R are obtained by adding multiples of 105 to the smallest value for R.
  • So in finding the smallest possible R of the above given form, we are in essence finding out R = Rem[3120/105]

By carefully introspecting the above 3 points, it is not difficult to understand how CRT really works.

By solving, we get R = 36 for (x,y,z) = (12,7,5)

Thus, Rem[3120/105] = 36


Note: In the above example and the definition given for CRT in the beginning of the article, the smaller individual divisors are taken to be coprimes to each other. For our objective, this is required for redundancy purposes but need not always be necessarily mandatory while solving using CRT.


-Angadbir

7 comments:

  1. Practice Problem for CRT:
    Find remainder when 2^1001 is divided by 105.

    ReplyDelete
  2. 2^1001 mod (15*7)

    E(15)=8

    2^1001 mod 15= 2^1 mod 15 = 15k+2

    E(7)=6

    2^2001 mod 7 = 2^5 mod 7=7m+5

    15k +2=7m+5

    k=2

    so ....rem =32

    ReplyDelete
    Replies
    1. Hey ashish..
      ur 1st part is correct but in 2nd part, 2^5 mod 7 = 4 and not 5...pls check again.
      so,15k= 7m +4.
      so, 15*4 = 7*8 +4+60...

      so..answer should be 60.

      Delete
    2. 32 is correct:-
      rem[2^1001/15]=[2x16^250]/15=2
      rem[2^1001/7]=[4x8^111]/7=4
      thus,R=15x+2=7y+4
      hit n trial, put x=2,y=4
      R=32.

      Delete
  3. if we have to find remainder of 3^120/25 then how can we apply CRT ?

    ReplyDelete
  4. Amit,

    CRT is usually applied when the divisor can be split into coprime factors.

    For the question you have stated, it is better to use Euler Totient Function.
    You can read up on it in the next article. Here: http://quantlogicfromzero.blogspot.com/2012/07/eulers-totient-and-remainder-problems.html

    ReplyDelete
  5. (76^23 x 48^59)/24^23
    how to solve this using rem theorem

    ReplyDelete