TT4–TT7 - Theorems of Fermat, Euler and Wilson
Calculate and search with congruences. Use Fermat's little theorem, Euler's totient and theorem, and Wilson's theorem with their exact conditions; distinguish a necessary primality condition from an exact test.
Keep the remainder, change the representative
A program finds . Does that make prime?
It is tempting to reverse a familiar theorem. This lesson will show why that conclusion fails, and which theorem really does give an if-and-only-if prime test.
You already know
The preceding lesson used integer division and remainders in Python. Primes exceed ; two integers are co-prime when their greatest common divisor, or HCF, is .
Congruence expresses the common remainder without requiring the original integers to be equal.
Congruent modulo a positive integer
For an integer modulus , means that divides : the integers have the same remainder on division by . The required final representative satisfies .
For example, . Both name the same residue, but is the canonical final answer. Use a congruence sign between equivalent representatives; is not literally equal to , although .
Calculating with congruences
If and , then
Non-negative integer powers may also be reduced before or after powering. Arbitrary division or cancellation is not justified: cancel a factor modulo only when it is co-prime to .
Not in the formula booklet: learn it.
Reduce early to keep calculations small, then reduce the final result once more.
Worked example 1: a sum
Question. Find the canonical residue of modulo .
Plan. Replace both large integers by their remainders.
Why this step? Congruence preserves addition and multiplication, so smaller representatives give the same final residue.
Answer. .
Check. .
Change only the addition sign to subtraction. The intermediate remainder becomes negative.
Worked example 2: a negative intermediate value
Question. Find modulo .
Plan. Use the same representatives, then bring the answer into the required range.
Why this step? A negative representative is useful during working, but the final answer must lie between and .
Answer. .
Check. .
Cancellation has a real boundary: has residues . Cancelling while retaining modulus would lose a solution; here .
Watch out
Wrong final answer modulo : . Right: . Equivalent representatives are allowed in working; final answers use .
Reduce a new subtraction.
Search one complete set of residues
To solve a congruence modulo , it is enough to check . Every other integer is congruent to exactly one of these representatives.
The program below returns every solution to , for integer . The list starts empty; append retains a successful candidate, and the function returns the complete list after the loop.
def roots_minus_one(n):
answers = []
for x in range(n):
if (x * x) % n == n - 1:
answers.append(x)
return answers
Using for matches Python's non-negative remainder for a positive modulus.
Worked example 3: two roots in one residue range
Question. Solve , giving all canonical residues.
Plan. Search the complete residue range; use symmetry only to shorten the displayed checking.
For , the squares repeat those for , because . The successful values are and .
Why this step? The entire residue range must be covered; finding one root does not finish an all-solutions question.
Answer. .
Check. and .
Change only the modulus from to . The target representative changes with it.
Worked example 4: another modulus
Question. Solve .
Plan. Check representatives through and their negatives.
The value occurs at , and by symmetry at .
Why this step? The condition is now a remainder of , not the previous modulus's target .
Answer. .
Check. and .
Predict, run, then change the modulus from to . Both runs should agree with the complete searches.
Search a complete residue range
{"code":"def roots_minus_one(n):\n answers = []\n for x in range(n):\n if (x * x) % n == n - 1:\n answers.append(x)\n return answers\n\nprint(roots_minus_one(13))","instructions":"Predict and run. Replace 13 by 17 in the call, predict the new list and run again. Explain why range(n) includes every canonical candidate exactly once."}
A complete modular search settles every residue class, even though it tests only finitely many representatives.
Watch out
Wrong: stop at the first successful root when asked for all roots. Right: retain every success and finish the complete residue range.
Adapt the target as well as the modulus.
A prime modulus shortens an exponent
Fermat's little theorem replaces a large exponent by blocks of , provided the modulus is prime and the base is co-prime to it.
Fermat's little theorem
If is prime and , then
Thus if , with and , then . The related form holds for every integer .
Not in the formula booklet: learn it.
State the conditions before reducing the exponent. A divisible base behaves differently: , not .
Worked example 5: many complete blocks
Question. Find modulo .
Plan. Check the conditions and split into blocks of .
Why this step? The blocks have length , not .
Answer. .
Check. The remaining calculation is .
Increase only the exponent by , so one additional factor remains.
Worked example 6: one more factor
Question. Find modulo .
Plan. Use the same conditions and a new exponent remainder.
Why this step? Increasing the exponent by one multiplies the previous residue by the base, then requires another reduction.
Answer. .
Check. .
The hook contains a surprising trap: is composite, yet , so . Passing this single Fermat-style test does not prove primality.
Watch out
Wrong: reverse Fermat's theorem to claim that a successful power test proves a number prime. Right: prime and co-prime are hypotheses; the converse need not hold.
Apply the theorem at a new prime modulus.
Count the co-prime residues
Euler's totient counts which residues can play the role needed in a more general exponent theorem.
Euler's totient
For , is the number of positive integers less than that are co-prime to . The conventional extension is .
Counting gives the same result when , because . It also implements that conventional value at .
Totient from prime factors
If the distinct prime factors of are , then
In particular, for prime and integer . Each distinct prime contributes one factor, regardless of its exponent.
Not in the formula booklet: learn it.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account