A logbook for curious waters
← All Reckonings

The Daily Reckoning

Why Does a Prime Make Giant Powers Loop?

Fermat’s little theorem turns multiplication modulo a prime into a collision-free rearrangement—and reduces 2¹⁰⁰⁰ to one small remainder.

Arithmetic Behind Secure Messages · Part 2 of 3

Difficulty
Intermediate
Area
Numbers and algebra
Reading time
9 minutes
Prerequisites
Modular arithmetic and congruence · Prime numbers · Products and exponents
Two columns of residues from 1 through 6 glow over a dark maritime drafting grid, joined by six crossing paths that show multiplication by 3 modulo 7 rearranging every residue exactly once.

The number 210002^{1000} has 302 decimal digits. What remainder does that number leave after division by 1313?

One route is to calculate all 302 digits and divide. A theorem about prime numbers reaches the answer using 24=162^4=16 instead. The theorem works because multiplication by a nonzero remainder modulo a prime cannot make two different remainders collide.

This lesson continues yesterday’s introduction to modular arithmetic. Congruence notation such as 29≡3(mod13)29\equiv3\pmod{13} says that 2929 and 33 leave the same remainder after division by 1313.

Six passengers, six seats

Start with the six nonzero remainders modulo 77:

1,2,3,4,5,6.1,2,3,4,5,6.

Multiply each remainder by 33, then keep only the remainder after division by 77:

3,6,2,5,1,4.3,6,2,5,1,4.

Every number from 11 through 66 appears once in the second list. Multiplication by 33 changed the seating order without losing a passenger or putting two passengers in one seat.

The diagram below draws every assignment from the first list to the second. Change the prime modulus or the multiplier. The lines cross in different places, but each output circle still receives exactly one arrow.

Interactive residue map

Multiply every nonzero remainder by the same number

Choose a prime modulus
Multiplication by 3 modulo 7Six arrows map the source residues 1 through 6 to the output residues 3, 6, 2, 5, 1, and 4. Each output receives exactly one arrow.SOURCE xOUTPUT 3x mod 7123456123456

×3 sends 1, 2, 3, 4, 5, 6 to 3, 6, 2, 5, 1, 4 modulo 7.

Repeated outputs
0
Landings on zero
0
Power consequence
3⁶ ≡ 1 (mod 7)
  1. 1 → 3
  2. 2 → 6
  3. 3 → 2
  4. 4 → 5
  5. 5 → 1
  6. 6 → 4
What to notice: every output circle receives one arrow. For each available multiplier, multiplication changes the order of the nonzero residues, but it neither duplicates a residue nor sends one to zero. Choose another prime or multiplier to test the claim.

Why are collisions impossible? Suppose two source remainders rr and ss land together after multiplication by 33. In symbols,

3r≡3s(mod7).3r\equiv3s\pmod7.

The congruence says that 77 divides 3(r−s)3(r-s). Because 77 is prime and does not divide 33, Euclid’s lemma says that 77 must divide r−sr-s. Both rr and ss lie between 11 and 66, so the only possible multiple of 77 for r−sr-s is 00. Therefore r=sr=s: the allegedly different sources were the same source.

No source can land on 00, either. If 3r≡0(mod7)3r\equiv0\pmod7, then 77 divides 3r3r. The prime 77 divides neither 33 nor any rr from 11 through 66, so that landing is impossible.

We now have six distinct outputs, and all six are chosen from the six nonzero remainders. The outputs must therefore be a rearrangement of 1,2,3,4,5,61,2,3,4,5,6.

The loop guaranteed by a prime

The same reasoning works for every prime modulus. Fermat’s little theorem states:

Let pp be a prime number, and let aa be an integer that pp does not divide. Then

ap−1≡1(modp).a^{p-1}\equiv1\pmod p.

The exponent p−1p-1 counts the nonzero remainders modulo pp. To prove the theorem, multiply those remainders by aa:

a,2a,3a,…,(p−1)a.a,2a,3a,\ldots,(p-1)a.

The collision argument used for 33 and 77 shows that these products, after reduction modulo pp, are a rearrangement of

1,2,3,…,p−1.1,2,3,\ldots,p-1.

Rearranging factors does not change their product, so

(a⋅1)(a⋅2)⋯(a⋅(p−1))≡1⋅2⋯(p−1)(modp),ap−1(p−1)!≡(p−1)!(modp).\begin{aligned} (a\cdot1)(a\cdot2)\cdots(a\cdot(p-1)) &\equiv1\cdot2\cdots(p-1)\pmod p,\\ a^{p-1}(p-1)!&\equiv(p-1)!\pmod p. \end{aligned}

The symbol (p−1)!(p-1)! means 1⋅2⋯(p−1)1\cdot2\cdots(p-1). None of those factors is divisible by pp, so their product has a multiplicative inverse modulo pp. Multiplying both sides by that inverse legally cancels (p−1)!(p-1)! and leaves

ap−1≡1(modp).a^{p-1}\equiv1\pmod p.

For a=3a=3 and p=7p=7, the proof says 36≡1(mod7)3^6\equiv1\pmod7. Six more factors of 33 will always return a power to the same remainder. The shortest cycle may be smaller than p−1p-1, but p−1p-1 is a guaranteed return time.

Worked example: 210002^{1000} modulo 1313

The modulus 1313 is prime, and 1313 does not divide 22. Fermat’s little theorem gives

212≡1(mod13).2^{12}\equiv1\pmod{13}.

Next, divide the exponent 10001000 by 1212:

1000=12⋅83+4.1000=12\cdot83+4.

Rewrite the power using that quotient and remainder:

21000=212⋅83+4=(212)8324≡183⋅16(mod13)≡3(mod13).\begin{aligned} 2^{1000} &=2^{12\cdot83+4}\\ &=(2^{12})^{83}2^4\\ &\equiv1^{83}\cdot16\pmod{13}\\ &\equiv3\pmod{13}. \end{aligned}

Therefore the 302-digit number 210002^{1000} leaves remainder 3 after division by 1313.

Try the prime shortcut

Problem 1

Find the remainder when 520265^{2026} is divided by 1111.

Reveal the solution

The number 1111 is prime, and 1111 does not divide 55, so

510≡1(mod11).5^{10}\equiv1\pmod{11}.

Reduce the exponent:

2026=10⋅202+6.2026=10\cdot202+6.

Therefore

52026≡56(mod11).5^{2026}\equiv5^6\pmod{11}.

Compute the sixth power in small steps:

52=25≡3(mod11),54≡32=9(mod11),56=54⋅52≡9⋅3=27≡5(mod11).\begin{aligned} 5^2&=25\equiv3\pmod{11},\\ 5^4&\equiv3^2=9\pmod{11},\\ 5^6&=5^4\cdot5^2\equiv9\cdot3=27\equiv5\pmod{11}. \end{aligned}

The remainder is 5.

Problem 2

Find a multiplicative inverse of 33 modulo 77. In other words, find a number bb for which 3b≡1(mod7)3b\equiv1\pmod7.

Reveal the solution

Fermat’s little theorem gives

36≡1(mod7).3^6\equiv1\pmod7.

Write 363^6 as 3⋅353\cdot3^5:

3⋅35≡1(mod7).3\cdot3^5\equiv1\pmod7.

Therefore 353^5 is an inverse of 33. Reduce it modulo 77:

35=243=7⋅34+5,3^5=243=7\cdot34+5,

so 35≡5(mod7)3^5\equiv5\pmod7. A direct check confirms the result:

3⋅5=15≡1(mod7).3\cdot5=15\equiv1\pmod7.

One multiplicative inverse is 5.

A theorem passed by letter, then proved by an academy mathematician

Pierre de Fermat stated the result in an October 1640 letter to Bernard Frénicle de Bessy, one of the few correspondents who shared Fermat’s interest in number theory. Fermat did not include a proof. The omission fit his usual mathematical practice: Fermat posed results and challenges in private correspondence far more readily than he prepared finished publications.

That habit was tied to Fermat’s working life. He was a lawyer and government official who purchased an office as a councillor in the Parlement of Toulouse by 1631. His mathematical network ran through letters among figures such as Pierre de Carcavi and Marin Mersenne. A civil war known as the Fronde, official duties, and a plague that nearly killed Fermat later interrupted those exchanges. The theorem emerged from an elite European correspondence network rather than from a university post or a mathematical journal.

Leonhard Euler wrote the first published proof in 1736. The St Petersburg Academy’s proceedings printed it in 1741 under the title A Proof of Certain Theorems Regarding Prime Numbers. The Euler Archive’s record of the paper identifies it as the first proof and dates both composition and publication.

Euler’s institutional position differed from Fermat’s. Euler joined the St Petersburg Academy of Sciences in 1727, became a full academy member in 1730, and took its senior mathematics chair in 1733. The academy supplied salary, colleagues, publication machinery, and state projects ranging from cartography to shipbuilding. Fermat’s correspondence allowed the claim to travel; the academy system gave Euler’s proof a durable printed home.

The rearrangement proof in this lesson is not Euler’s 1736 argument. Euler’s paper instead advances through successive bases and mathematical induction.

Where the prime loop is used

Modular division. Ordinary division is not available for every remainder system. Fermat’s little theorem constructs inverses modulo a prime: ap−2a^{p-2} is an inverse of aa because a⋅ap−2=ap−1≡1(modp)a\cdot a^{p-2}=a^{p-1}\equiv1\pmod p. Computer algebra, coding theory, and cryptographic algorithms use these inverses to solve modular equations.

Primality screening. If a candidate integer nn and a test base aa satisfy an−1≢1(modn)a^{n-1}\not\equiv1\pmod n, then nn is certainly composite. Passing the test does not prove that nn is prime; some composite numbers imitate prime behavior for one base, and Carmichael numbers do so for every base coprime to them. Current standards therefore use stronger checks. The U.S. National Institute of Standards and Technology’s Digital Signature Standard, FIPS 186-5, specifies Miller–Rabin and Lucas probable-prime tests when generating primes for certain digital-signature and Rivest–Shamir–Adleman (RSA) keys.

Public-key cryptography. The prime-number case developed here is one ingredient in the arithmetic behind RSA. The Internet Engineering Task Force’s RSA specification defines encryption and decryption primitives using modular powers. Fermat’s theorem alone does not explain why the private exponent reverses the public exponent when the modulus is a product of primes. Part three will supply Euler’s broader theorem and work through that reversal with small numbers.

One idea to remember

Multiplication by a nonzero remainder modulo a prime can only rearrange the nonzero remainders. Multiplying the whole rearrangement together forces ap−1≡1(modp)a^{p-1}\equiv1\pmod p, which turns enormous exponents into manageable ones.

Continue with modular arithmetic and repeating residue cycles, or return to why multiplying by ii makes a quarter-turn for another example of multiplication acting as a transformation. Part three of Arithmetic Behind Secure Messages will replace the prime modulus with the composite modulus used by RSA.

Sources