Application Center - Maplesoft

App Preview:

High School Advanced Topics - Modular Arithmetic

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Modular Arithmetic.mws

High School Modules > Advanced Topics

     Modular Arithmetic


An exploration of modular arithmetic - adding, multiplying, powers, orders, residues.

[Directions : Execute the Code Resource section first. Although there will be no output immediately, these definitions are used later in this worksheet.]

    0. Code

>    restart; with(plots):

Warning, the name changecoords has been redefined

>    ModTable  :=  proc(n)
    local A,i,j,k,m;
    m := floor( n*3/2 + 3);
    A := array( [seq( [  seq(` `, j = 1..(n) ) ],   i = 1..m) ]);
    A[1,1] := cat(`mod `,n);       
    for k from 1 to n do  A[2,   k] :=  k-1; od;
    for k from 1 to n do  A[3,   k] :=  `__`; od;
  
 
    for i from 1 to m-3 do  
        for j from 1 to n do  
             A[i+3,j] :=  (n*(i-1)+j-1)  ;   od;od;
   
    print(A);
    end proc:

>    ModAddTable  :=  proc(n)
    local A,i,j,k;
    
    A := array( [seq( [  seq(` `, j = 1..(n+3) ) ],   i = 1..(n+3)) ]);
    A[1,1] := n;        A[2,2] := `+`;
    for k from 1 to n do  A[1,k+3] :=  k-1; od;
    for k from 1 to n do  A[k+3, 1] :=  k-1; od;
    for k from 0 to n do  A[2,k+3] :=  `__`; od;
    for k from 0 to n do  A[k+3, 2] :=  `|`; od;
 
    for i from 1 to n do  
        for j from 1 to n do  
             A[i+3,j+3] :=  (i + j -2) mod n ;   od;od;
   
    print(A);
    end proc:

>    ModMultTable  :=  proc(n)
    local A,i,j,k;
    
    A := array( [seq( [  seq(` `, j = 1..(n+3) ) ],   i = 1..(n+3)) ]);
    A[1,1] := n;        A[2,2] := `*`;
    for k from 1 to n do  A[1,k+3] :=  k-1; od;
    for k from 1 to n do  A[k+3, 1] :=  k-1; od;
    for k from 0 to n do  A[2,k+3] :=  `__`; od;
    for k from 0 to n do  A[k+3, 2] :=  `|`; od;
 
    for i from 1 to n do  
        for j from 1 to n do  
             A[i+3,j+3] :=  ((i-1)*(j-1)) mod n ;   od;od;
   
    print(A);
    end proc:

>    ModPowTable  :=  proc(n)
    local A,i,j,k;
    
    A := array( [seq( [  seq(` `, j = 1..(n+2) ) ],   i = 1..(n+2)) ]);
    A[1,1] := cat(`mod `,n);        A[1,2] := `powers :`;  A[2,1] := `k :`;
    for k from 1 to n do  A[1,k+2] :=  k; od;
    for k from 1 to n do  A[k+2, 1] :=  k-1; od;
    for k from 1 to n do  A[2,k+2] :=  `__`; od;
    for k from 1 to n do  A[k+2, 2] :=  `|`; od;
 
    for i from 1 to n do  
        for j from 1 to n do  
             A[i+2,j+2] :=  (i-1)^j mod n ;   od;od;
   
    print(A);
    end proc:

    1. Modular Equivalence & Reduction


Modular arithmetic, or clock arithmetic, reduces all positive integers to a set of remainders. This is done relative to some fixed integer n. For example, lets consider numbers "modulo 5". This means any integer is equivalent to the remainder of that number divided by 5. In this context, we don't care about the quotient, only the remainder.

>    17 mod 5;

2

>    irem( 17, 5);

2


This is called "clock arithmetic" sometimes because it works similar to the way a clock works - cycling through the same numbers.

>    for k from 1 to 25 do k,` mod 12 is `, k mod 12; od;

1, ` mod 12 is `, 1

2, ` mod 12 is `, 2

3, ` mod 12 is `, 3

4, ` mod 12 is `, 4

5, ` mod 12 is `, 5

6, ` mod 12 is `, 6

7, ` mod 12 is `, 7

8, ` mod 12 is `, 8

9, ` mod 12 is `, 9

10, ` mod 12 is `, 10

11, ` mod 12 is `, 11

12, ` mod 12 is `, 0

13, ` mod 12 is `, 1

14, ` mod 12 is `, 2

15, ` mod 12 is `, 3

16, ` mod 12 is `, 4

17, ` mod 12 is `, 5

18, ` mod 12 is `, 6

19, ` mod 12 is `, 7

20, ` mod 12 is `, 8

21, ` mod 12 is `, 9

22, ` mod 12 is `, 10

23, ` mod 12 is `, 11

24, ` mod 12 is `, 0

25, ` mod 12 is `, 1

>    for k from 1 to 19 do k,` mod 5 is `, k mod 5; od;

1, ` mod 5 is `, 1

2, ` mod 5 is `, 2

3, ` mod 5 is `, 3

4, ` mod 5 is `, 4

5, ` mod 5 is `, 0

6, ` mod 5 is `, 1

7, ` mod 5 is `, 2

8, ` mod 5 is `, 3

9, ` mod 5 is `, 4

10, ` mod 5 is `, 0

11, ` mod 5 is `, 1

12, ` mod 5 is `, 2

13, ` mod 5 is `, 3

14, ` mod 5 is `, 4

15, ` mod 5 is `, 0

16, ` mod 5 is `, 1

17, ` mod 5 is `, 2

18, ` mod 5 is `, 3

19, ` mod 5 is `, 4


You can see that the results at right goes through the same 5 numbers : 0, 1, 2, 3, and 4 - like a clock with only 5 hours ( and that starts at 0 rather than 1).

A regular clock shows the same time 12 hours later as it does 24, 36, 48, 60, 72, ... hours later.

>    3 mod 12;
15 mod 12;
27 mod 12;
39 mod 12;

3

3

3

3


 Here are the numbers from 0 to 69, mod 7. Basically its a remainder chart. For any number in the body of the table, you can look at the top row to see what its remainder would be modulo 7. All of the numbers in each column are all equivalent mod 7!

>    ModTable(7);

matrix([[`mod 7`, ` `, ` `, ` `, ` `, ` `, ` `], [0, 1, 2, 3, 4, 5, 6], [__, __, __, __, __, __, __], [0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25...


Here are the numbers from 0 to 23, mod 4. Again, all of the numbers in each column can be reduced to the remainder in the top row.

>    ModTable(4);

matrix([[`mod 4`, ` `, ` `, ` `], [0, 1, 2, 3], [__, __, __, __], [0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]])

>    ModTable(13);

matrix([[`mod 13`, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [__, __, __, __, __, __, __, __, __, __, __, __, __], [0, 1, 2, 3, 4, 5, 6, 7...


 

    2. Modular Addition


When adding two numbers, and the result is less than n (the modular base), then the numbers simple add. However, if the result

>    2 + 3;  % = % mod 7;

5

5 = 5

>    (5 + 3);  % = % mod 7;

8

8 = 1

>    (5 + 2);  % = % mod 7;

7

7 = 0


    3. Modular Addition Tables


We can make addition tables modulo a number. Note that 0 + k = k.

>    ModAddTable(3);

matrix([[3, ` `, ` `, 0, 1, 2], [` `, `+`, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `], [0, `|`, ` `, 0, 1, 2], [1, `|`, ` `, 1, 2, 0], [2, `|`, ` `, 2, 0, 1]])

>    ModAddTable(4);

matrix([[4, ` `, ` `, 0, 1, 2, 3], [` `, `+`, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `], [0, `|`, ` `, 0, 1, 2, 3], [1, `|`, ` `, 1, 2, 3, 0], [2, `|`, ` `, 2, 3, 0, 1], [3, `|`, ` `, 3,...

>    ModAddTable(5);

matrix([[5, ` `, ` `, 0, 1, 2, 3, 4], [` `, `+`, __, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `, ` `], [0, `|`, ` `, 0, 1, 2, 3, 4], [1, `|`, ` `, 1, 2, 3, 4, 0], [2, `|`, ` `, 2, 3, 4, 0,...

>    ModAddTable(11);

matrix([[11, ` `, ` `, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [` `, `+`, __, __, __, __, __, __, __, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `], [0, `|`, ` ...


    4. Modular Multiplication


Modular multiplication is simply multiplying two integers - then reduce modularly.

>    3*4; % mod 13;

12

12

>    3*5; % mod 13;

15

2

>    6*5; % mod 13;

30

4


 

    5. Modular Multiplication Tables


We can make multiplication tables too.

>    ModMultTable(2);

matrix([[2, ` `, ` `, 0, 1], [` `, `*`, __, __, __], [` `, `|`, ` `, ` `, ` `], [0, `|`, ` `, 0, 0], [1, `|`, ` `, 0, 1]])

>    ModMultTable(5);

matrix([[5, ` `, ` `, 0, 1, 2, 3, 4], [` `, `*`, __, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `, ` `], [0, `|`, ` `, 0, 0, 0, 0, 0], [1, `|`, ` `, 0, 1, 2, 3, 4], [2, `|`, ` `, 0, 2, 4, 1,...

>    ModMultTable(11);

matrix([[11, ` `, ` `, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [` `, `*`, __, __, __, __, __, __, __, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `], [0, `|`, ` ...

>    ModMultTable(15);

matrix([[15, ` `, ` `, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [` `, `*`, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], [` `, `|`, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ` `, ...


 

    6. Modular Powers


Similar to products, we can find modular powers, by find the power, then reducing modularly.

>    3^2; % mod 17;

9

9

>    3^3; % mod 17;

27

10

>    for k from 1 to 12 do
       '3'^k = 3^k mod 17;
od;

3 = 3

9 = 9

27 = 10

81 = 13

243 = 5

729 = 15

2187 = 11

6561 = 16

19683 = 14

59049 = 8

177147 = 7

531441 = 4


 

    7. Modular Power Tables


We can easily create tables of the powers. The numbers along the right column are the integers. The powers are in the top row.

>    ModPowTable(3);

matrix([[`mod 3`, `powers :`, 1, 2, 3], [`k :`, ` `, __, __, __], [0, `|`, 0, 0, 0], [1, `|`, 1, 1, 1], [2, `|`, 2, 1, 2]])

>    ModPowTable(5);

matrix([[`mod 5`, `powers :`, 1, 2, 3, 4, 5], [`k :`, ` `, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 3, 1, 2], [3, `|`, 3, 4, 2, 1, 3], [4, `|`, 4, 1, 4, 1, ...

>    ModPowTable(7);

matrix([[`mod 7`, `powers :`, 1, 2, 3, 4, 5, 6, 7], [`k :`, ` `, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 1, 2, 4, 1, 2], [3, `|`, 3, 2,...

>    ModPowTable(12);

matrix([[`mod 12`, `powers :`, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [`k :`, ` `, __, __, __, __, __, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1...


 

    8. The Order of a Number


Please take a moment to look for some patterns in this table.

>    ModPowTable(5);

matrix([[`mod 5`, `powers :`, 1, 2, 3, 4, 5], [`k :`, ` `, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 3, 1, 2], [3, `|`, 3, 4, 2, 1, 3], [4, `|`, 4, 1, 4, 1, ...



A few observations :
        
1 .     k = k      (of course), this is why the first column of the table (under 1) matches the original numbers.
        
2.     k^p = k     a bit more surprising - the last column of the table (under 5) matches the original numbers too.
                           This is true modulo a prime number.
     
   3.     k^h = 1     for all non-zero numbers at some point - but that point varies. The minimal exponents that give
                           1 are : 1, 4, 4, 2 ... all divisors of p-1 = 5-1 = 4.  ( 1^1 = 1,  
2^4  =1, 3^4  = 1, 4^2  = 1).


The smallest positive integer h, for which k to the h is one, is called the
order   of k mod p .  We can find the order directly using a Maple command.       

>    with(numtheory):

Warning, the protected name order has been redefined and unprotected

>    order(2,5); order(4,5);

4

2

>    for k from 1 to 5 do `order of `, k, ` is `, order(k,5); od;

`order of `, 1, ` is `, 1

`order of `, 2, ` is `, 4

`order of `, 3, ` is `, 4

`order of `, 4, ` is `, 2

`order of `, 5, ` is `, FAIL

5 fails because that is congruent to zero mod 5.

Find the order of the numbers 1 through 6 mod 7 :

>    ModPowTable(7);

matrix([[`mod 7`, `powers :`, 1, 2, 3, 4, 5, 6, 7], [`k :`, ` `, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 1, 2, 4, 1, 2], [3, `|`, 3, 2,...



Find the order of the numbers 1 through 10 mod 11 :

>    ModPowTable(11);

matrix([[`mod 11`, `powers :`, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [`k :`, ` `, __, __, __, __, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1, 1, ...



What if n is not a prime number?

>    ModPowTable(6);

matrix([[`mod 6`, `powers :`, 1, 2, 3, 4, 5, 6], [`k :`, ` `, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 2, 4, 2, 4], [3, `|`, 3, 3, 3, 3, 3, 3], [4...


Many of the numbers never become one by raising them to powers. Which numbers do have this property? And what distinguishes them?

The only numbers which can be raised to one, are 1 and 5. The numbers 2, 3, and 4 never become one. The distinction is that 1 and 5 are relatively prime to 6, while 2, 3, and 4 have a gcd greater than one.

Lets look at some other examples. What non-zero integers less than 8 are relatively prime to 8? 1, 3, 5, and 7. We would expect to see that these can be raised to powers that make them one.

>    ModPowTable(8);

matrix([[`mod 8`, `powers :`, 1, 2, 3, 4, 5, 6, 7, 8], [`k :`, ` `, __, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 0, 0, 0, 0, 0, 0]...



For 9, the relatively prime numbers are 1, 2, 4, 5, 7, and 8.

>    ModPowTable(9);

matrix([[`mod 9`, `powers :`, 1, 2, 3, 4, 5, 6, 7, 8, 9], [`k :`, ` `, __, __, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 8, 7...


 

    9. Multiplicative Inverse


In the world of real numbers, every non-zero number has a multiplicative inverse. Given a number a, there is a number b, such that ab = 1. Often we call b the
reciprocal  of a.

>    4 * (1/4);

1

>    (2/41) * (41/2);

1

>    (.0125) * 80;

1.0000

>    -31 * (-1/31);

1


In the modular world, we don't have fractions so this is not possible. However, under what conditions are there other integers which serve the same purpose as being multiplicative inverses?

For example, take the number 3, mod 7. Is there an integer such at 3*b = 1 (mod 7)? Yes, 5 is the "reciprocal of 3 mod 7",

>    3;

3

>    3*5 ;% = % mod 7;

15

15 = 1


How can we better understand this process? A quick consideration of the numbers mod 7 once again  :

>    ModPowTable(7);

matrix([[`mod 7`, `powers :`, 1, 2, 3, 4, 5, 6, 7], [`k :`, ` `, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 1, 2, 4, 1, 2], [3, `|`, 3, 2,...



Since n = 7 is prime, every non-zero number 1,2, 3, 4, 5, 6 has an order - an exponent h, such that k^h = 1. This leads to an interesting idea. Given any such number k, multiplying by
k^(h-1)  gives    k*k^(h-1) = k^h = 1.

Using this idea we can find the inverse for 3 mod 7.

>    h := order(3, 7);

h := 6

>    3^(h-1);
% mod 7;

243

5

>    3*5;
% mod 7;

15

1



Find the reciprocal of 5 mod 11.

>    h := order( 5, 11);

h := 5

>    5^(h-1); % mod 11;

625

9

>    5*9; % mod 11;

45

1



Find the reciprocal of 8 mod 17.

>    h := order( 10, 17);

h := 16

>    10^(h-1); % mod 17;

1000000000000000

12

>    10*12; % mod 17;

120

1


When n is not prime, not every number has an inverse. However, those numbers relatively prime to the modular base do.
 Find the reciprocal of 8 mod 17.

>    h := order( 8, 15);

h := 4

>    8^(h-1); % mod 15;

512

2

>    8*2; % mod 15;

16

1

    10. Quadratic Residues


Just looking at the squares of integers mod 5, what is the set of values?

>    ModPowTable(5);

matrix([[`mod 5`, `powers :`, 1, 2, 3, 4, 5], [`k :`, ` `, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 3, 1, 2], [3, `|`, 3, 4, 2, 1, 3], [4, `|`, 4, 1, 4, 1, ...


The only non-zero squares are 1 and 4. These are called the quadratic residues - what is left after the multiples of 5 are boiled off, so to speak.

Only these numbers have modular square roots. For example there are two solutions to the equation :

>    x^2 = 4;

x^2 = 4

>    2^2 mod 5; 3^2 mod 5;

4

4


So the square roots of 4 mod 5 are 2 and 3.

What are the quadratic residues mod 7?

>    ModPowTable(7);

matrix([[`mod 7`, `powers :`, 1, 2, 3, 4, 5, 6, 7], [`k :`, ` `, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 1, 2, 4, 1, 2], [3, `|`, 3, 2,...


The residues mod 7 are  1, 2 and 4. What are the square roots of 2?

The residues mod 9 are  1, 4, 7. What are the square roots of 7?

>    ModPowTable(9);

matrix([[`mod 9`, `powers :`, 1, 2, 3, 4, 5, 6, 7, 8, 9], [`k :`, ` `, __, __, __, __, __, __, __, __, __], [0, `|`, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, `|`, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, `|`, 2, 4, 8, 7...

>   


 


          2002 Waterloo Maple Inc & Gregory Moore, all rights reserved.