Chris Collins

Tools · math

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

a

first number for Euclid and Bézout

a=1071

b

second number for Euclid and Bézout

b=462

c

Bézout: also solve b·x ≡ c (mod a)

c=3

jugs

jug capacities and the amount wanted: A,B,goal

jugs=3,5,4

w

sieve: grid width

w=6

max

sieve: last number

max=400

n

trial division: the number to factor

n=360

m

clock and tables: the modulus

m=12

x

clock: the starting number

x=9

y

clock: the number added (and the multiplier)

y=5

k

clock, chord mode: the multiplier k in x → kx

k=2

view

clock: clock or chord

view=chord

op

tables: mul or add

op=mul

num

% on negatives: the dividend

num=-7

den

% on negatives: the divisor

den=2

base

fast powers: the base

base=7

e

fast powers: the exponent (10^18 is fine)

e=13

mod

fast powers: the modulus, or none for plain powers

mod=11

p

decimals: numerator

p=1

q

decimals: denominator

q=7

lcg

LCG: a,c,m,seed

lcg=5,3,16,7

code

check digits: the code (spaces and dashes are ignored; ? marks a digit to solve for)

code=0-306-40615-2

scheme

check digits: luhn, isbn10 or isbn13

scheme=isbn10

dh

Diffie–Hellman: p,g,a,b

dh=23,5,6,15

rsa

RSA: p,q,e

rsa=5,11,3

msg

RSA: the message, a number below n or a word

msg=7

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

ch23-euclid-1071-462

euclid

gcd(1071, 462) = 21 as a trace table and as tiles (ch. 23)

ch23-tiles-48-18

euclid

the 48 × 18 floor tiled with squares: the gcd is 6

ch23-lcm-252-198

euclid

gcd 18 and lcm 2772

ch23-lame-89-55

euclid

Lamé’s worst case: consecutive Fibonacci numbers, 9 steps

ch02-gcd-126-84

euclid

simplifying 84/126 by Euclid (ch. 2)

ch23-bezout-1071-462

bezout

21 = 1071·(−3) + 462·7 by back-substitution and by the forward table

ch23-bezout-240-46

bezout

exercise: 2 = 240·(−9) + 46·47

ch23-mastery-391-299

bezout

mastery check: gcd 23 = 391·(−3) + 299·4

ch23-inverse-7-mod-26

bezout

7⁻¹ ≡ 15 (mod 26), then 7x ≡ 3 gives x ≡ 19

ch23-congruence-4x-6-mod-10

bezout

4x ≡ 6 (mod 10): gcd 2 divides 6, so x ∈ {4, 9}

ch23-jugs-die-hard

jugs

the Die Hard 3 puzzle: 4 gallons from 3 and 5

ch23-jugs-6-15

jugs

exercise: 6 and 15 can measure 9 but never 4

ch23-sieve-30

sieve

the worked example: primes up to 30, sieving with 2, 3 and 5

ch23-sieve-100

sieve

the figure: 1–100 on a 10-wide grid

ch23-sieve-6

sieve

width 6: primes after 3 sit in two columns, 6k ± 1

ch23-sieve-20x20

sieve

the lab’s 20 × 20 grid

ch01-trial-360

trial

360 = 2³ · 3² · 5, stopping once d² > 5 (ch. 1)

ch01-trial-97

trial

97 is prime: nothing up to √97 ≈ 9.85 divides it

ch23-trial-391

trial

is 391 prime? 391 = 17 · 23

ch23-clock-9-plus-5

clock

9 + 5 ≡ 2 (mod 12) as a walk round the dial

ch23-cardioid

clock

x → 2x mod 200 as chords: a cardioid

ch23-nephroid

clock

x → 3x mod 200: a nephroid

ch23-table-mod-7

table

mod 7, a prime: every nonzero row has a 1

ch23-table-mod-8

table

mod 8: 2, 4 and 6 have no inverse and hit 0

ch23-zero-divisors-mod-6

table

2·3 ≡ 0 (mod 6): why you cannot cancel

ch23-negative-7-2

negative

−7 % 2: Python says 1, C says −1

ch23-negative-17-5

negative

exercise: −17 % 5 in C and in Python

ch23-power-7-13-mod-11

power

7¹³ mod 11 = 2, bit by bit

ch23-power-race

power

the exponentiation race: e = 10¹⁸, about 90 steps against 10¹⁸

ch23-power-3-45-mod-17

power

mastery check: 3⁴⁵ mod 17 = 12

ch03-power-3-13

power

3¹³ by repeated squaring, no modulus (ch. 3)

ch03-power-2-21

power

exercise: 2²¹ = 2 097 152

ch03-power-7-10-mod-13

power

exercise: 7¹⁰ mod 13 = 4

ch02-decimal-1-7

decimal

1/7: the remainders 1, 3, 2, 6, 4, 5 come round again

ch02-decimal-3-22

decimal

3/22 = 0.1(36): a digit before the cycle

ch02-decimal-7-12

decimal

7/12 = 0.58(3)

ch23-lcg-5-3-16

lcg

x → (5x + 3) mod 16 visits all 16 states

ch23-lcg-c4

lcg

c = 4 breaks Hull–Dobell: short cycles

ch23-lcg-lattice

lcg

m = 256: the pairs fall on a few lines

ch23-isbn-0-306-40615-2

check

the ISBN-10 worked example: sum 132 = 12 · 11

ch23-isbn-mastery

check

mastery check: is 0-19-853453-1 valid?

ch23-luhn-7992-7398-713

check

the Luhn worked example: total 70

ch23-dh-23-5

dh

p = 23, g = 5, secrets 6 and 15: shared secret 2

ch23-rsa-5-11

rsa

tiny RSA: n = 55, d = 27, 7 → 13 → 7

ch23-rsa-61-53

rsa

the textbook example: n = 3233, d = 2753, 65 → 2790

ch23-rsa-word

rsa

letter by letter: both Ls encrypt the same

ch23-rsa-mastery

rsa

mastery check: n = 33, d = 7, 4 → 31 → 4

Runs entirely in your browser; nothing you type leaves this page.