High School Mathematics Extensions/Primes/Modular Arithmetic
|Problems & Projects|
|Problem Set Solutions|
Modular Arithmetic 
Modular arithmetic connects with primes in an interesting way. Modular arithmetic is a system in which all numbers up to some positive integer, n say, are used. So if you were to start counting you would go 0, 1, 2, 3, ... , n - 1 but instead of counting n you would start over at 0. And what would have been n + 1 would be 1 and what would have been n + 2 would be 2. Once 2n has been reached the number is reset to 0 again, and so on. Modular arithmetic is also called clock-arithmetic because we only use 12 numbers to tell standard time. On clocks we start at 1 instead of 0, continue to 12, and then start at 1 again. Hence the name clock-arithmetic.
The sequence also continues into what would be the negative numbers. What would have been -1 is now n - 1. For example, consider modulo 7 arithmetic, it's just like ordinary arithmetic except the only numbers we use are 0, 1, 2, 3, 4, 5 and 6. If we see a number outside of this range we add 7 to (or subtract 7 from) it, until it lies within that range.
As mentioned above, modular arithmetic is not much different than ordinary arithmetic. For example in modulo 7 arithmetic, we have
We have done some calculation with negative numbers. Consider 5 × -6. Since -6 does not lie in the range 0 to 6, we need to add 7 to it until it does. And -6 + 7 = 1. So in modular 7 arithmetic, -6 = 1. In the above example we showed that 5 × -6 = -30 = 5, but 5 × 1 = 5. So we didn't do ourselves any harm by using -6 instead of 1. Why?
Note - Negatives: The preferred representation of -3 is 4, as -3 + 7 = 4, but using either -3 and 4 in a calculation will give us the same answer as long as we convert the final answer to a number between 0 and 6 (inclusive).
Find in modulo 11
- -1 × -5
- 3 × 7
3. Compute the first 10 the powers of 2
- 21, 22, 23, ... , 210
What do you notice?
Using the powers of 2 find
- 61, 62, 63, ... , 610
What do you notice again?
i.e. find, by trial and error (or otherwise), all numbers x such that x2 = 4 (mod 11). There are two solutions, find both .
i.e. find all numbers x such that x2 = 9 (mod 11). There are two solutions, find both.
Consider a number n, the inverse of n is the number that when multiplied by n gives 1. For example, if we were to solve the following equation
the (mod 7) is used to make it clear that we are doing arithemetic modulo 7. We want to get rid of the 5 somehow. Multiplying it by something to turn it into a 1 would do the job. Notice that
because 3 multiplied by 5 gives 1, we say 3 is the inverse of 5 in modulo 7. Now we multiply both sides by 3
So x = 2 modulo 7 is the required solution.
- The inverse of (a number) x is a number y such that xy = 1. We denote the inverse of x by x-1 or 1/x.
Inverse is unique
From above, we know the inverse of 5 is 3, but does 5 have another inverse? The answer is no. In fact, in any reasonable number system, a number can have one and only one inverse. We can see that from the following proof
Suppose n has two inverses b and c
From the above argument, all inverses of n must be equal. As a result, if the number n has an inverse, the inverse must be unique.
An interesting property of any modulo n arithmetic is that the number n - 1 has itself as an inverse. That is, (n - 1) × (n - 1) = 1 (mod n), or we can write (n - 1)2 = (-1)2 = 1 (mod n). The proof is left as an exercise at the end of the section.
Existence of inverse 
Not every number has an inverse in every modulo arithmetic. For example, 3 doesn't have an inverse mod 6, i.e., we can't find a number x such that 3x = 1 mod 6 (the reader can easily check).
Consider modulo 15 arithmetic and note that 15 is composite. We know the inverse of 1 is 1 and of 14 is 14. But what about 3, 6, 9, 12, 5 and 10? None of them has an inverse! Note that each of them shares a common factor with 15!
As an example, we show that 3 does not have an inverse modulo 15. Suppose 3 has an inverse x, then we have
We make the jump from modular arithemetic into rational number arithmetic. If 3x = 1 in modulo 15 arithmetic, then
for some integer k. Now we divide both sides by 3, we get
But this cannot be true, because we know that x is an integer, not a fraction. Therefore 3 doesn't have an inverse in mod 15 arithmetic. To show that 10 doesn't have an inverse is harder and is left as an exercise.
We will now state the theorem regarding the existence of inverses in modular arithmetic.
- If n is prime then every number (except 0) has an inverse in modulo n arithmetic.
- If n is composite then every number that doesn't share a common factor with n has an inverse.
It is interesting to note that division is closely related to the concept of inverses. Consider the following expression
the conventional way to calculate the above would be to find the inverse of 3 (being 5). So
We write the inverse of 3 as 1/3, so we think of multiplying 3-1 as dividing by 3, we get
Notice that we got the same answer! In fact, the division method will always work if the inverse exists.
However, the expression in a different modulo system will produce the wrong answer, for example
we don't get 2, as 3-1 does not exist in modulo 9, so we can't use the division method.
1. Does 8 have an inverse in mod 16 arithemetic? If not, why not?
2. Find x mod 7 if x exists:
3. Calculate x in two ways, finding inverse and division
4. (Trick) Find x
5. Find all inverses mod n (n ≤ 19)
Coprime and greatest common divisor 
Two numbers are said to be coprimes if their greatest common divisor (gcd) is 1. E.g. 21 and 55 are both composite, but they are coprime as their greatest common divisor is 1, or in another they do not share a common divisor other than 1.
There is a quick and elegant way to compute the gcd of two numbers, called Euclid's algorithm. Let's illustrate with a few examples:
- Find the gcd of 21 and 49.
We set up a two-column table where the larger of the two numbers is on the right hand side as follows
We now compute 49 (mod 21) which is 7 and put it in the second row smaller column, and put 21 into the larger column.
Perform the same action on the second row to produce the third row.
Whenever we see the number 0 appear on the smaller column, we know the corresponding larger number is the the gcd of the two numbers we started with, i.e. 7 is the gcd of 21 and 49. This algorithm is called Euclid's algorithm.
- Find the gcd of 31 and 101
- Find the gcd of 132 and 200
Important to note
- The gcd need not be a prime number.
- The gcd of two different primes is 1. In other words, two different primes are always coprime.
1. Determine whether the following sets of numbers are coprimes
- 5050 5051
- 59 78
- 111 369
- 2021 4032
2. Find the gcd of the numbers 15, 510 and 375
info -- Algorithm 
An algorithm is a step-by-step description of a series of actions when performed correctly can accomplish a task. There are algorithms for finding primes, deciding whether 2 numbers are coprimes, finding inverses and many other purposes.
You'll learn how to implement some of the algorithms we have seen using a computer in the chapter [[../../../Mathematical Programming/]].
Finding Inverses 
Let's look at the idea of inverse again, but from a different angle. In fact we will provide a sure-fire method to find the inverse of any number. Let's consider:
- 5x = 1 (mod 7)
We know x is the inverse of 5 and we can work out it is 3 reasonably quickly. But x = 10 is also a solution, so is x = 17, 24, 31, ... 7n + 3. So there are infinitely many solutions; therefore we say 3 is equivalent to 10, 17, 24, 31 and so on. This is a crucial observation.
Now let's consider
A new notation is introduced here, it is the equal sign with three strokes instead of two. It is the "equivalent" sign; the above statement should read "216x is EQUIVALENT to 1" instead of "216x is EQUAL to 1". From now on, we will use the equivalent sign for modulo arithmetic and the equal sign for ordinary arithmetic.
Back to the example, we know that x exists, as gcd(811,216) = 1. The problem with the above question is that there is no quick way to decide the value of x! The best way we know is to multiply 216 by 1, 2, 3, 4... until we get the answer, there are at most 816 calculations, way too tedious for humans. But there is a better way, and we have touched on it quite a few times!
We notice that we could make the jump just like before into rational mathematics:
We jump into rational maths again
We jump once more
Now the pattern is clear, we shall start from the beginning so that the process is not broken:
Now all we have to do is choose a value for f and substitute it back to find a! Remember a is the inverse of 216 mod 811. We choose f = 0, therefore e = 1, d = 13, c = 40, b = 53 and finally a = 199! If f is chosen to be 1 we will get a different value for a.
The very perceptive reader should have noticed that this is just Euclid's gcd algorithm in reverse.
Here are a few more examples of this ingenious method in action:
Find the smallest positive value of a:
Choose d = 0, therefore a = 49.
Example 2 Find the smallest positive value of a:
Choose e = 0, therefore a = -152 = 669
Example 3 Find the smallest positive value of a:
Set i = 0, then a = -21 = 34. Why is this so slow for two numbers that are so small? What can you say about the coefficients?
Example 4 Find the smallest positive value of a:
Now d is not an integer, therefore 21 does not have an inverse mod 102.
What we have discussed so far is the method of finding integer solutions to equations of the form:
- ax + by = 1
where x and y are the unknowns and a and b are two given constants, these equations are called linear Diophantine equations. It is interesting to note that sometimes there is no solution, but if a solution exists, it implies that infinitely many solutions exist.
Diophantine equation 
In the Modular Arithmetic section, we stated a theorem that says if gcd(a,m) = 1 then a-1 (the inverse of a) exists in mod m. It is not difficult to see that if p is prime then gcd(b,p) = 1 for all b less than p, therefore we can say that in mod p, every number except 0 has an inverse.
We also showed a way to find the inverse of any element mod p. In fact, finding the inverse of a number in modular arithmetic amounts to solving a type of equations called Diophantine equations. A Diophantine equation is an equation of the form
- ax + by = d
where x and y are unknown.
As an example, we should try to find the inverse of 216 in mod 811. Let the inverse of 216 be x, we can write
we can rewrite the above in every day arithmetic
which is in the form of a Diophantine equation.
Now we are going to do the inelegant method of solving the above problem, and then the elegant method (using Magic Tables).
Both methods mentioned above uses the Euclid's algorithm for finding the gcd of two numbers. In fact, the gcd is closely related to the idea of an inverse. Let's apply the Euclid's algorithm on the two numbers 216 and 811. This time, however, we should store more details; more specifically, we want to set up an additional column called PQ which stands for partial quotient. The partial quotient is just a technical term for "how many n goes into m" e.g. The partial quotient of 3 and 19 is 6, the partial quotient of 4 and 21 is 5 and one last example the partial quotient of 7 and 49 is 7.
The tables says three 216s goes into 811 with remainder 163, or symbollically:
- 811 = 3×216 + 163.
Reading off the table, we can form the following expressions
- 811 = 3× 216 + 163
- 216 = 1× 163 + 53
- 163 = 3× 53 + 4
- 53 =13× 4 + 1
Now that we can work out the inverse of 216 by working the results backwards
- 1 = 53 - 13×4
- 1 = 53 - 13×(163 - 3×53)
- 1 = 40×53 - 13×163
- 1 = 40×(216 - 163) - 13×163
- 1 = 40×216 - 53×163
- 1 = 40×216 - 53×(811 - 3×216)
- 1 = 199×216 - 53×811
Now look at the equation mod 811, we will see the inverse of 216 is 199.
Magic Table 
The Magic Table is a more elegant way to do the above caculations, let us use the table we form from Euclid's algorithm
Now we set up the so-called "magic table" which looks like this initially
Now we write the partial quotient on the first row:
We produce the table according to the following rule:
- Multiply a partial quotient one space to the left of it in a different row, add the product to the number two space to the left on the same row and put the sum in the corresponding row.
It sounds more complicated then it should. Let's illustrate by producing a column:
We put a 3 in the second row because 3 = 3×1 + 0. We put a 1 in the third row because 1 = 3×0 + 1.
We shall now produce the whole table without disruption:
We can check that
- |199×216 - 811×53| = 1
In fact, if the magic table is contructed properly, and we cross multiplied and subtracted the last two column correctly, then we will always get 1 or -1, provided the two numbers we started with were coprimes. The magic table is just a cleaner way of doing the mathematics.
1. Find the smallest positive x:
2. Find the smallest positive x:
(a) Produce the magic table for 33a = 1 (mod 101)
(b) Evaluate and express in the form p/q
What do you notice?
(a) Produce the magic table for 17a = 1 (mod 317)
(b) Evaluate and express in the form p/q
What do you notice?
Chinese remainder theorem 
The Chinese remainder theorem is known in China as Han Xing Dian Bing, which in its most naive translation means Han Xing counts his soldiers. The original problem goes like this:
- There exists a number x, when divided by 3 leaves remainder 2, when divided by 5 leaves remainder 3 and when divided by 7 leaves remainder 2. Find the smallest x.
We translate the question into symbolic form:
How do we go about finding such a x? We shall use a familiar method and it is best illustrated by example:
Looking at x = 2 (mod 3), we make the jump into ordinary mathematics
Now we look at the equation modulo 5
Substitute into (1) to get the following
Now look at the above modulo 7
We choose b = 1 to minimise x, therefore x = 23. And a simple check (to be performed by the reader) should confirm that x = 23 is a solution. A good question to ask is what is the next smallest x that satisfies the three congruences? The answer is x = 128, and the next is 233 and the next is 338, and they differ by 105, the product of 3, 5 and 7.
We will illustrate the method of solving a system of congruences further by the following examples:
Example 1 Find the smallest x that satifies:
now substitute back into the first equation, we get
again substituting back
Therefore 52 is the smallest x that satisfies the congruences.
Example 2 
Find the smallest x that satisfies:
now solve for b
again, substitue back
Therefore 269 is the smallest x that satisfies the congruences.
1. Solve for x
2. Solve for x
*Existence of a solution* 
The exercises above all have a solution. So does there exist a system of congruences such that no solution could be found? It certainly is possible, consider:
- x ≡ 5 (mod 15)
- x ≡ 10 (mod 21)
a cheekier example is:
- x ≡ 1 (mod 2)
- x ≡ 0 (mod 2)
but we won't consider silly examples like that.
Back to the first example, we can try to solve it by doing:
the above equation has no solution because 3 does not have an inverse modulo 21!
One may be quick to conclude that if two modulo systems share a common factor then there is no solution. But this is not true! Consider:
we can find a solution
we now multiply both sides by the inverse of 5 (which is 17), we obtain
obviously, k = 3 is a solution, and the two modulo systems are the same as the first example (i.e. 15 and 21).
So what determines whether a system of congruences has a solution or not? Let's consider the general case:
essentially, the problem asks us to find k and l such that the above equations are satisfied.
We can approach the problem as follows
now suppose m and n have gcd(m,n) = d, and m = dmo, n = dno. We have
if (a - b)/d is an integer then we can read the equation mod mo, we have:
Again, the above only makes sense if (a - b)/d is integeral. Also if (a - b)/d is an integer, then there is a solution, as mo and no are coprimes!
In summary: for a system of two congruent equations
there is a solution if and only if
- d = gcd(m,n) divides (a - b)
And the above generalises well into more than 2 congruences. For a system of n congruences:
for a solution to exist, we require that if i ≠ j
- gcd(mi,mj) divides (ai - aj)
Decide whether a solution exists for each of the congruences. Explain why.
- x ≡ 7 (mod 25)
- x ≡ 22 (mod 45)
- x ≡ 7 (mod 23)
- x ≡ 3 (mod 11)
- x ≡ 3 (mod 13)
- x ≡ 7 (mod 25)
- x ≡ 22 (mod 45)
- x ≡ 7 (mod 11)
- x ≡ 4 (mod 28)
- x ≡ 28 (mod 52)
- x ≡ 24 (mod 32)
To go further 
This chapter has been a gentle introduction to number theory, a profoundly beautiful branch of mathematics. It is gentle in the sense that it is mathematically light and overall quite easy. If you enjoyed the material in this chapter, you would also enjoy Further Modular Arithmetic, which is a harder and more rigorous treatment of the subject.
Also, if you feel like a challenge you may like to try out the Problem Set we have prepared for you. On the other hand, the project asks you to take a more investigative approach to work through some of the finer implications of the Chinese Remainder Theorem.
Acknowledgement: This chapter of the textbook owes much of its inspiration to Terry Gagen, Emeritus Associate Professor of Mathematics at the University of Sydney, and his lecture notes on "Number Theory and Algebra". Terry is a much loved figure among his students and is renowned for his entertaining style of teaching.
What do you think? Too easy or too hard? Too much information or not enough? How can we improve? Please let us know by leaving a comment in the discussion section. Better still, edit it yourself and make it better.