Modular Arithmetic Clock

Choose modulus n and operation. See result on a modular clock and inspect addition/multiplication tables.

Loading interactive simulation...

A prime modulus lets you divide again 🖖

Modular arithmetic deals with integers under a cyclic modulus n, written as a ≑ b (mod n). This algebraic system forms a commutative ring Z/nZ. If the modulus p is prime, the ring becomes a field, meaning every non-zero element has a multiplicative inverse. This structure underlies modern cryptography, cyclic codes, and clock arithmetic, demonstrating how numbers behave under periodic boundaries.

Why it is called clock arithmetic 🖖

On a 12-hour clock, 4 hours after 11 o'clock is 3, not 15 β€” the numbers wrap back to the start once they pass 12. That is exactly modular arithmetic: a ≑ b (mod n) means a and b leave the same remainder when divided by n. So 15 ≑ 3 (mod 12). The core takeaway: modular arithmetic keeps only the remainder after division, and this tool's clock lets you watch the numbers loop around.

The nine-divisibility trick is hidden modular arithmetic 🖖

The old rule "a number is divisible by 9 if its digits sum to a multiple of 9" is pure modular arithmetic. Because 10 ≑ 1 (mod 9), every power of 10 is also ≑ 1, so any number is congruent to the sum of its digits (mod 9). The same fact powers "casting out nines", a centuries-old way to check arithmetic by hand.

MODULAR ARITHMETIC β€” WHEN CAN YOU DIVIDE, AND WHEN CAN YOU NOT?

Which Modular Arithmetic Case Are You In?

Working mod n means keeping only the remainder, and addition, subtraction and multiplication all survive that unharmed. Division is the one that does not. Whether you can divide by a number β€” whether it has an inverse β€” depends entirely on whether it shares a factor with n, which is why a prime modulus behaves so differently from a composite one. Which case you are in depends on the operation and on what kind of number n is.

Addition β€” always well behaved, whatever n is 13 + 5 ≡ 6 (mod 12)
A prime modulus β€” every nonzero element can be divided by φ(7) = 6 ⇒ gcd(a, 7) = 1 ∀a ≠ 0
A composite modulus β€” most elements cannot be divided by 2 × 3 ≡ 0, φ(6) = 2
Repeated powers β€” they cycle, and Fermat says where ap−1 ≡ 1 (mod p)

01

Addition β€” always well behaved, whatever n is

What you know: Any modulus at all. Adding, subtracting and multiplying mod n are always defined, and every element has an additive opposite.

What to check: 13 + 5 ≡ 6 (mod 12)

Worked example: A 12-hour clock: 13 + 5 = 18, and 18 mod 12 = 6. Five hours after one o'clock is six o'clock.

Open this case: clock mod 12
Addition β€” always well behaved, whatever n is. The addition table mod 12: every row is the previous one shifted along by one, and every value appears exactly once. Any modulus at all. Adding, subtracting and multiplying mod n are always defined, and every element has an additive opposite.
The addition table mod 12: every row is the previous one shifted along by one, and every value appears exactly once.

02

A prime modulus β€” every nonzero element can be divided by

What you know: n is prime. Then no nonzero element shares a factor with n, so every one of them has a multiplicative inverse.

What to check: φ(7) = 6 ⇒ gcd(a, 7) = 1 ∀a ≠ 0

Worked example: Mod 7: 3 Γ— 4 = 12 ≑ 5. All six nonzero values 1…6 are invertible, so the multiplication table has no zeros away from the top row.

Open this case: prime mod 7
A prime modulus β€” every nonzero element can be divided by. The multiplication table mod 7: no zeros below the first row, and each row a permutation of 1 to 6. n is prime. Then no nonzero element shares a factor with n, so every one of them has a multiplicative inverse.
The multiplication table mod 7: no zeros below the first row, and each row a permutation of 1 to 6.

03

A composite modulus β€” most elements cannot be divided by

What you know: n is composite. Only the values coprime to n have inverses; the rest are zero divisors, and dividing by them is meaningless.

What to check: 2 × 3 ≡ 0, φ(6) = 2

Worked example: Mod 6: 2 Γ— 4 = 8 ≑ 2. Neither 2 nor 4 is invertible, since gcd(2,6) = 2 and gcd(4,6) = 2. Only 1 and 5 are units β€” two out of six.

Open this case: composite mod 6
A composite modulus β€” most elements cannot be divided by. The multiplication table mod 6, with zeros scattered through it and only two rows that are permutations. n is composite. Only the values coprime to n have inverses; the rest are zero divisors, and dividing by them is meaningless.
The multiplication table mod 6, with zeros scattered through it and only two rows that are permutations.

04

Repeated powers β€” they cycle, and Fermat says where

What you know: Raising to a power mod n. For prime p and a not divisible by p, the exponents wrap with period dividing p βˆ’ 1.

What to check: ap−1 ≡ 1 (mod p)

Worked example: Mod 13: 7ΒΉΒ² ≑ 1. Fermat's little theorem guarantees it for every base from 1 to 12, without computing a single large power.

Open this case: fermat mod 13
Repeated powers β€” they cycle, and Fermat says where. The powers of each base mod 13, with the column at exponent 12 coming back to 1 for every unit. Raising to a power mod n. For prime p and a not divisible by p, the exponents wrap with period dividing p βˆ’ 1.
The powers of each base mod 13, with the column at exponent 12 coming back to 1 for every unit.

Problem solved in full

  1. Evaluating 7 12 mod 13 on the full grid 5 steps

    Evaluate 712 mod 13 without ever writing 712 down. This is the ak operation with n = 13, a = 7 and k = 12, and the grid is the full table of ak mod 13.

    1. 712 is 13 841 287 201 β€” eleven digits for an answer that has to come out between 0 and 12. Reduction survives multiplication: the residue of a product depends only on the residues of its factors, so you may reduce after every step rather than at the end.

    2. Fermat's little theorem settles the answer before any arithmetic happens. For a prime p and an a that p does not divide, apβˆ’1 ≑ 1; here p = 13 and k is exactly p βˆ’ 1, so the result is 1 and everything below is a check on the theorem rather than a search for the answer.

    3. Squaring doubles the exponent, so three squarings reach 78. Each line reduces before the next one starts, which is why no number in the whole calculation exceeds 100.

    4. 12 is 8 + 4, and both of those powers were computed on the way up, so one more multiplication finishes it. Four multiplications in total, against the eleven that a left-to-right product of twelve sevens would need β€” and the saving grows with the exponent, not with the modulus.

    5. The order of 7, meaning the least k with 7k ≑ 1, must divide 12, so it is one of 1, 2, 3, 4, 6 and 12. Checking the five proper divisors rules every one of them out, so the order is exactly 12.

    Answer

    The three squarings and the closing multiplication reproduce the tool's row for a = 7 without an error: 10 at k = 2, 9 at k = 4, 3 at k = 8, and 1 at k = 12 where Fermat said it would be. Because the order is 12 and not a proper divisor of it β€” the order test needed 12 at k = 6, which is not 1 β€” that row is a permutation of 1 through 12, hitting every non-zero residue exactly once, which makes 7 a primitive root mod 13. Now read the row in the other direction: given 11, find k. There is no square-and-multiply for that, only a search, and that asymmetry between easy forwards and hard backwards is the whole basis of Diffie–Hellman key exchange. The algorithm scales in a way the table cannot: a 2048-bit exponent costs at most about 4 000 modular multiplications, while the power it names would take roughly 2.7 Γ— 10616 digits to write out.

Learning path

When two things land on the same value

Leads to Birthday paradox

References (1)

Example problems

  • clock mod 12 - Clock arithmetic: 13 ≑ 1 (mod 12), so 13+5 wraps to 6.
  • prime mod 7 - Prime modulus: nonzero multiplication rows behave as permutations.
  • composite mod 6 - Composite modulus shows repeating rows where gcd(row,n) > 1.
  • fermat mod 13 - Fermat-style pattern: a^(p-1) ≑ 1 mod p for prime p and gcd(a,p)=1.