Number theory step by step
The working behind the answers — Euclid as a trace table and as square tiles, extended Euclid with back-substitution, the two-jug puzzle, an animated sieve, trial division, clock arithmetic and its cardioids, mod-m tables, % on negative numbers, square-and-multiply against naive powers, repeating decimals, LCGs, check digits, Diffie–Hellman and RSA.
Added
Works with
- Bookshelf Everydaychecks ISBN-10 and ISBN-13 check digits like these as you scan and type books into a catalogue
How to use it
The Number theory tool gives answers. This one shows the working, one step at a time, the way you would write it out by hand. Each tab is one procedure. Wherever there is a list of steps, Step through shows them one by one so you can try the next one yourself first. All the integer arithmetic is exact (JavaScript BigInt), so the Euclid, Bézout, power, Diffie–Hellman and RSA tabs work with numbers of any size.
-
Euclid — every division written out (
1071 = 2·462 + 147, with the multiplication and subtraction beside it), the trace table (a, b, q, r), the gcd, the lcm and the reduced fraction. Below it, the same steps as tiling an a × b rectangle with the largest squares that fit. Lamé’s worst case (consecutive Fibonacci numbers) is pointed out. -
Bézout — the divisions solved for their remainders, then back-substitution from the gcd line upwards (substitute, expand, collect), then the forward table (q, r, x, y with a check column) that code uses. When the gcd is 1 it reads off the inverse of b mod a. Fill in c to solve the congruence b·x ≡ c (mod a), including the case where the gcd is more than 1.
-
Jugs — fill, empty and pour between jugs of capacity A and B, or watch the shortest solution. The amounts strip shades every reachable amount (the multiples of gcd(A, B)), and when you hit the goal the tool counts your fills and empties as a Bézout combination.
-
Sieve — the sieve of Eratosthenes on a grid of any width, animated: each prime crosses out its multiples from p² in its own colour. Column headers show each column’s remainder; columns that share a factor with the width are marked, so width 6 shows why primes past 3 are 6k ± 1. The table of rounds matches the book’s worked example.
-
Trial division — d = 2, 3, 4, … with a table of every try, a number line with the divisor moving towards the √ marker (which jumps left as factors come out), and a plot of divisors tried against n, where the primes form the top envelope.
-
Clock — a dial for any modulus with x + y as a walk and x · y as y hops of x (extra laps drawn as inward rings), each with its division line. Chords joins every x to kx mod m: k = 2 on 200 points is a cardioid, 3 a nephroid. k may be fractional, and Sweep k animates it.
-
Mod tables — the multiplication (or addition) table mod m with inverses (the 1s) and zero divisors (0s in nonzero rows) coloured, the units listed with their inverses, and why you cannot cancel. Click a cell for its arithmetic.
-
% on negatives — truncated, floored and Euclidean division side by side with a = b·q + r checked, what each of 17 languages and functions prints, a chart of the remainder against a, and the two bugs it causes.
-
Fast powers — the exponent’s bits as switches, the book’s square-and-multiply table lit a bit at a time, the operation count against naive multiplication, and a race that runs the naive loop for real and estimates how long it would take. With no modulus it follows chapter 3’s version (no wasted last square).
-
Repeating decimals — long division with a remainder wheel: each step lights the remainder it lands on, the cycle turns red when a remainder comes round again, and the period is shown, with the conversion back to a fraction and a chart of the period of 1/q for q up to 50.
-
LCG — x → (a·x + c) mod m: the period and every cycle, the state graph (cycle in blue, tail in orange), the low bit’s pattern, and the scatter of consecutive pairs that shows the lattice. The three Hull–Dobell conditions are listed with Break it / Fix it buttons that change a or c as little as possible.
-
Check digits — Luhn, ISBN-10 and ISBN-13 as weighted tables with the sum and its remainder. Type
?for a digit to have it solved. For a valid code it tries every single-digit error and every swap of neighbours and lists the ones the scheme misses. -
Diffie–Hellman — Alice, the public wire and Bob side by side, each power with its square-and-multiply table, the order of g, and Eve trying every exponent.
-
RSA — key generation with every step shown (prime checks, n and φ, gcd(e, φ) by Euclid, d by back-substitution and the forward table), encryption and decryption of a number or of a word letter by letter (repeated letters light up), and an attacker who factors n and times bigger keys.
Numbers can be typed with a little arithmetic: 10^18, 2^64+1, 1e6, 3*7, -17. In a link a
` arrives as a space, so a space between two numbers is read as ` (e=2^64 1 is 264 + 1);
writing %2B also works.
Limits
Sieve grids go up to 2000; trial division gives up after 3 million divisors (what is left then has no factor that small); the jugs go up to 2000; mod tables up to 40; clocks up to 360 and chord pictures up to 2000 points; repeating decimals up to q = 100 000; LCGs up to m = 65 536 (the state graph is drawn for m ≤ 64). Powers without a modulus are limited to about 120 000 digits. Prime checks use Miller–Rabin with 13 fixed bases, which is exact below 3.3 × 1024. The RSA and Diffie–Hellman here are textbook versions for learning: deterministic, unpadded, and not safe for anything real.
Related: Number theory (answers only: primality, factoring, CRT, continued fractions), Algebra step by step, Sets, relations and quantifiers (congruence mod m as an equivalence relation, divisibility as a partial order), Boolean logic, CRC (polynomial remainders mod 2).
Companion to chapter 23 of Mathematics, Reconnected, Integers, divisibility and modular arithmetic (the book), with the trial division of chapter 1, the Euclid and repeating decimals of chapter 2 and the exponentiation by squaring of chapter 3.
Linking to an example
Share link (under the title) copies a link to the tool exactly as you have it. You can also write
one by hand: add parameters after ? and the tab after #, for example
?a=1071&b=462#bezout or
?base=3&e=1018&mod=1000000007#power.
The tabs are euclid, bezout, jugs, sieve, trial, clock, table, negative, power,
decimal, lcg, check, dh and rsa. A link replaces your saved settings for this tool (a
Restore my settings button undoes it). Parameters you leave out take their defaults.
| Parameter | Meaning | Example |
|---|---|---|
|
first number for Euclid and Bézout |
|
|
second number for Euclid and Bézout |
|
|
Bézout: also solve b·x ≡ c (mod a) |
|
|
jug capacities and the amount wanted: A,B,goal |
|
|
sieve: grid width |
|
|
sieve: last number |
|
|
trial division: the number to factor |
|
|
clock and tables: the modulus |
|
|
clock: the starting number |
|
|
clock: the number added (and the multiplier) |
|
|
clock, chord mode: the multiplier k in x → kx |
|
|
clock: clock or chord |
|
|
tables: mul or add |
|
|
% on negatives: the dividend |
|
|
% on negatives: the divisor |
|
|
fast powers: the base |
|
|
fast powers: the exponent (10^18 is fine) |
|
|
fast powers: the modulus, or none for plain powers |
|
|
decimals: numerator |
|
|
decimals: denominator |
|
|
LCG: a,c,m,seed |
|
|
check digits: the code (spaces and dashes are ignored; ? marks a digit to solve for) |
|
|
check digits: luhn, isbn10 or isbn13 |
|
|
Diffie–Hellman: p,g,a,b |
|
|
RSA: p,q,e |
|
|
RSA: the message, a number below n or a word |
|
example= opens one of the book’s worked examples or labs, and also picks its tab when the link has no #. Other parameters in the same link override the example’s values (?example=ch23-rsa-5-11&msg=HELLO).
example= |
Tab | What it shows |
|---|---|---|
euclid |
gcd(1071, 462) = 21 as a trace table and as tiles (ch. 23) |
|
euclid |
the 48 × 18 floor tiled with squares: the gcd is 6 |
|
euclid |
gcd 18 and lcm 2772 |
|
euclid |
Lamé’s worst case: consecutive Fibonacci numbers, 9 steps |
|
euclid |
simplifying 84/126 by Euclid (ch. 2) |
|
bezout |
21 = 1071·(−3) + 462·7 by back-substitution and by the forward table |
|
bezout |
exercise: 2 = 240·(−9) + 46·47 |
|
bezout |
mastery check: gcd 23 = 391·(−3) + 299·4 |
|
bezout |
7⁻¹ ≡ 15 (mod 26), then 7x ≡ 3 gives x ≡ 19 |
|
bezout |
4x ≡ 6 (mod 10): gcd 2 divides 6, so x ∈ {4, 9} |
|
jugs |
the Die Hard 3 puzzle: 4 gallons from 3 and 5 |
|
jugs |
exercise: 6 and 15 can measure 9 but never 4 |
|
sieve |
the worked example: primes up to 30, sieving with 2, 3 and 5 |
|
sieve |
the figure: 1–100 on a 10-wide grid |
|
sieve |
width 6: primes after 3 sit in two columns, 6k ± 1 |
|
sieve |
the lab’s 20 × 20 grid |
|
trial |
360 = 2³ · 3² · 5, stopping once d² > 5 (ch. 1) |
|
trial |
97 is prime: nothing up to √97 ≈ 9.85 divides it |
|
trial |
is 391 prime? 391 = 17 · 23 |
|
clock |
9 + 5 ≡ 2 (mod 12) as a walk round the dial |
|
clock |
x → 2x mod 200 as chords: a cardioid |
|
clock |
x → 3x mod 200: a nephroid |
|
table |
mod 7, a prime: every nonzero row has a 1 |
|
table |
mod 8: 2, 4 and 6 have no inverse and hit 0 |
|
table |
2·3 ≡ 0 (mod 6): why you cannot cancel |
|
negative |
−7 % 2: Python says 1, C says −1 |
|
negative |
exercise: −17 % 5 in C and in Python |
|
power |
7¹³ mod 11 = 2, bit by bit |
|
power |
the exponentiation race: e = 10¹⁸, about 90 steps against 10¹⁸ |
|
power |
mastery check: 3⁴⁵ mod 17 = 12 |
|
power |
3¹³ by repeated squaring, no modulus (ch. 3) |
|
power |
exercise: 2²¹ = 2 097 152 |
|
power |
exercise: 7¹⁰ mod 13 = 4 |
|
decimal |
1/7: the remainders 1, 3, 2, 6, 4, 5 come round again |
|
decimal |
3/22 = 0.1(36): a digit before the cycle |
|
decimal |
7/12 = 0.58(3) |
|
lcg |
x → (5x + 3) mod 16 visits all 16 states |
|
lcg |
c = 4 breaks Hull–Dobell: short cycles |
|
lcg |
m = 256: the pairs fall on a few lines |
|
check |
the ISBN-10 worked example: sum 132 = 12 · 11 |
|
check |
mastery check: is 0-19-853453-1 valid? |
|
check |
the Luhn worked example: total 70 |
|
dh |
p = 23, g = 5, secrets 6 and 15: shared secret 2 |
|
rsa |
tiny RSA: n = 55, d = 27, 7 → 13 → 7 |
|
rsa |
the textbook example: n = 3233, d = 2753, 65 → 2790 |
|
rsa |
letter by letter: both Ls encrypt the same |
|
rsa |
mastery check: n = 33, d = 7, 4 → 31 → 4 |
Feedback is a gift. Something to improve, a feature to add, or a bug? Tell me.
Runs entirely in your browser; nothing you type leaves this page.