Skip to content

← Math Further 9–12

How Many Eggs Were in the Basket?

Topic 8 · Grades 9–12 · The Chinese remainder theorem

What this builds on from class
In this unit 10 sections
  1. Try it with a smaller basket
  2. Counting in threes, fives and sevens
  3. Why there is always exactly one
  4. Sunzi’s recipe
  5. When the divisors share a factor
  6. More baskets
  7. A story from Sunzi to Gauss
  8. Where this is used
  9. Questions
  10. Where to go next

A woman sells eggs at the Saturday market in Bhaktapur, her basket on a stool beside the road. A motorbike swerves round a cow, clips the stool, and the basket goes over. Every egg breaks. The rider is sorry and wants to pay for them all, but how many were there? She can’t remember the number. She only remembers this, from packing them that morning:

  • Taken out 2 at a time, there was 1 egg left over.
  • The same with 3 at a time, 4, 5 and 6 at a time: always 1 left over.
  • Taken out 7 at a time, they came out even, none left over.

She is sure the basket held fewer than 400. How many eggs must the rider pay for?

Try it

Count the eggs

Choose a number of eggs with the slider (the − 1 and + 1 buttons fine-tune it). Each row shows the eggs taken out in twos, threes, and so on, and the eggs left over. A row that fits what the woman remembers turns green. Find a number that turns all six green.

In 2s25 = 12 × 2 + 1In 3s25 = 8 × 3 + 1In 4s25 = 6 × 4 + 1In 5s25 = 5 × 5 + 0none leftIn 6s25 = 4 × 6 + 1In 7s25 = 3 × 7 + 4
Rows that fit
4 of 6
Baskets that fit all six
none found yet

Not yet. You want 1 egg left over in every row except the last, where the sevens must come out even.

Guess first, before you search. How many eggs could the basket have held, below 400?

Pick an answer to see why.

Try it with a smaller basket

Trying every number up to 400 is slow. Start with only some of the rules and look for a pattern.

Only twos and threes. Which numbers leave 1 egg over when taken out in twos, and also 1 over in threes? Try 1, 2, 3, … with the widget, or in your head. You find 1, 7, 13, 19, 25, …: they go up in steps of 6.

  • a.Why steps of 6? Before reading on, try to say why.
  • b.Now add the fours. Which numbers leave 1 over in twos, threes and fours? What is the step now?
  • c.And with twos, threes, fours, fives and sixes, all leaving 1?

That is the trick for the first five rules: they all say the same thing, take one egg away and the rest is a multiple of 60. That is the lowest common multiple at work.

Now only the sevens are left. Go along 1, 61, 121, 181, … and divide each by 7:

Eggs 1 61 121 181 241 301
Left over in sevens 1 5 2 6 3 0

301 eggs: 301 = 43 × 7, and 300 is a multiple of 60, so 301 leaves 1 over in twos, threes, fours, fives and sixes.

And why does the next answer come 420 eggs later? It must be a multiple of 60 more than 301 and a multiple of 7 more, so a multiple of their LCM, 420: 301, 721, 1,141, …. Her “fewer than 400” picks out one.

A trader’s sack of walnuts leaves 1 over when counted in twos, threes and fours, and none over when counted in fives. The sack holds fewer than 50. How many walnuts?

Pick an answer to see why.

Counting in threes, fives and sevens

The eggs were kind: five of the six rules said the same thing. In the oldest puzzle of this kind we know of, every remainder is different. It comes from a Chinese book written some time between the 3rd and 5th centuries (no one knows exactly when), the Sunzi suanjing (“Master Sun’s Mathematical Manual”, chapter 3, problem 26). In our own words:

There are some things, we don’t know how many. Counted in threes, 2 are left over. Counted in fives, 3 are left over. Counted in sevens, 2 are left over. How many things are there?

The quickest way by hand is to go up in the biggest steps. The numbers that leave 2 over in sevens are 2, 9, 16, 23, 30, …. Check each against the fives (3 left over) and the threes (2 left over): 2 leaves 2 in fives, 9 leaves 4, 16 leaves 1, and 23 leaves 3. And 23 = 7 × 3 + 2 leaves 2 in threes. So 23, and the next answers are 23 + 105, 23 + 210, …, since 3 × 5 × 7 = 105.

A page of an old Chinese printed book, columns of characters read from top to bottom
Figure 8.1 The opening page of the Sunzi suanjing, in a Qing dynasty printing: the title, then the units of length. The remainder puzzle comes in its third chapter. Photo: Unknown printer, Qing dynasty, Public domain

That is searching. Sunzi gives a recipe that turns any three remainders straight into the answer. Here it is as a trick: a friend thinks of a number from 0 to 104 and tells you only its remainders on dividing by 3, 5 and 7, and you name it.

Try it

Read my mind

Pick any number from 0 to 104 in your head, work out its three remainders, and tap them. Then try to work out how the page does it before you press “Show how”.

÷ 3 leaves
÷ 5 leaves
÷ 7 leaves
Your number is 23

But first a deeper question. Why should every choice of remainders belong to exactly one number from 0 to 104? There are 3 × 5 × 7 = 105 choices and 105 numbers; if two numbers shared their remainders, some choice would be left with none. The next section shows that never happens.

Why there is always exactly one

Take just two divisors, 3 and 5. Write each number in a table: the row is what it leaves on dividing by 3, the column what it leaves on dividing by 5. The number 7 leaves 1 in threes and 2 in fives, so it goes in row 1, column 2.

Try it

Remainders as a table

Slide to count up from 0 and watch where each number lands. Then try the other pairs.

Left over in 5sLeft over in 3s0123400631174252
7 leaves
1 in 3s, 2 in 5s
On the square of
7
Squares filled
8 of 15
Back at 0 at
15

Counted up to 14, the numbers fill all 15 squares, one each: every pair of remainders by 3 and by 5 happens exactly once before the walk is back at 0.

Try these with the widget.

  • a.With 3 and 5, how does each number move from the one before? What happens at the edges?
  • b.Does any square of the 3 × 5 table get two numbers from 0 to 14? Does any stay empty?
  • c.Now 4 and 6. What is different?

Here is why 3 and 5 can’t share a square between two numbers from 0 to 14. Suppose two of them, say m and n, landed on the same square. Then they leave the same remainder on dividing by 3, so their difference n − m is a multiple of 3; and they leave the same remainder on dividing by 5, so n − m is a multiple of 5 too. A multiple of both 3 and 5 is a multiple of their LCM, 15. But two different numbers from 0 to 14 differ by at least 1 and at most 14: no multiple of 15 fits. So no two numbers from 0 to 14 share a square.

Now count. 15 numbers, 15 squares, and no square gets two: then no square is left empty either, like 15 people in 15 chairs with no one sharing. So every pair of remainders happens, exactly once, from 0 to 14.

The same argument works for any divisors that share no common factor. Two numbers that leave the same remainders differ by a multiple of every divisor, so by a multiple of their LCM; and when no two of the divisors share a factor, the LCM is their product, N. So no two numbers from 0 to N − 1 leave the same remainders; and since there are N numbers and N choices of remainders, the counting goes as before: every choice is taken, exactly once. That is the theorem.

It is called the Chinese remainder theorem, after Sunzi’s problem (sometimes Sunzi’s theorem). In the language of clock arithmetic: if x≡2(mod3) x \equiv 2 \pmod 3 , x≡3(mod5) x \equiv 3 \pmod 5 and x≡2(mod7) x \equiv 2 \pmod 7 , then x≡23(mod105) x \equiv 23 \pmod{105} .

What it means: a number from 0 to 104 and its three remainders are two ways of writing the same thing. Computers use exactly that (below): work with small remainders, and rebuild the big number only at the end.

A number from 0 to 34 leaves 3 on dividing by 5 and 6 on dividing by 7. How many such numbers are there?

Pick an answer to see why.

Sunzi’s recipe

The counting proof says the answer exists, but not how to find it without searching. Sunzi’s recipe does: multiply the leftover in threes by 70, the leftover in fives by 21, the leftover in sevens by 15, add, and take away 105 as often as you can. For his puzzle: 2 × 70 + 3 × 21 + 2 × 15 = 233, and 233 − 2 × 105 = 23.

The secret is in what 70, 21 and 15 leave:

Number Left over in threes in fives in sevens
70 1 0 0
21 0 1 0
15 0 0 1

Each is a building block that leaves 1 for one divisor and 0 for the others. 70 is a multiple of 5 and 7 that leaves 1 in threes; 21 is a multiple of 3 and 7 that leaves 1 in fives; 15 is a multiple of 3 and 5 that leaves 1 in sevens. So 2 × 70 leaves 2 in threes and 0 in fives and sevens; 3 × 21 leaves 3 in fives and 0 in the others; 2 × 15 leaves 2 in sevens. Add them, and each divisor sees only its own block: 233 leaves 2, 3 and 2. Taking away 105s changes no remainder, since 105 is a multiple of all three. It only brings the number down to between 0 and 104.

How do you find a block? For the threes, try multiples of 5 × 7 = 35: 35 leaves 2, 70 leaves 1. For the fives, 3 × 7 = 21 already leaves 1; for the sevens, 3 × 5 = 15. With big divisors, trying is slow; the block solves a linear Diophantine equation, 35k = 3y + 1, which Euclid’s algorithm run backwards solves at once (the kuttaka of topic 6, linked at the end).

When the divisors share a factor

The theorem needs divisors with no common factor. What goes wrong without it? Try this: a number leaves 1 over in fours and 2 over in sixes. Search as long as you like; there is none. A number that leaves 1 in fours is odd (4 is even, so one more than a multiple of 4 is odd). A number that leaves 2 in sixes is even. No number is both. The rules contradict each other, though each is fine alone.

Change the second rule to “3 over in sixes”, and now both say the number is odd. Then there are answers: 9, 21, 33, …, every 12, the LCM of 4 and 6, not every 24. That is the table for 4 and 6 in the widget: the squares reached are exactly those where the two remainders are both odd or both even. Why are all of those reached? Two numbers on the same square differ by a multiple of 4 and of 6, so of 12; so 0 to 11 land on 12 different squares. And there are exactly 12 squares whose remainders match like that (2 even rows × 3 even columns, and 2 odd rows × 3 odd columns). The walk fills every one of them.

The egg puzzle passed this test: its divisors 2, 3, 4, 5, 6 share plenty of factors, but every rule said “1 over”, so they agreed, and together said only “1 over in 60s”.

Could a Tibetan year ever be a male Fire-Ox year?

Pick an answer to see why.

More baskets

Always one short. A basket leaves 1 over in twos, 2 over in threes, 3 in fours, 4 in fives, 5 in sixes, and comes out even in sevens. Every remainder is one less than the divisor: add one egg and every count would come out even. So the number is one less than a multiple of 60: 59, 119, 179, …. The first that comes out even in sevens is 119 = 17 × 7.

Bhaskara II set almost the same puzzle in his Bijaganita (12th century), in its chapter on equations with several unknowns, solved with the kuttaka. In Colebrooke’s 1817 English translation: “What number is it, which, being divided by six, has five for a remainder; or divided by five, has a residue of four; or divided by four, has a remainder of three; or divided by three, leaves two?” Bhaskara’s working gives 59; his commentator Krishna adds that with the next choice it is 119.

Ibn al-Haytham’s answer. About a thousand years ago, Ibn al-Haytham (about 965–1040, Basra and Cairo, better known for his work on light) asked for exactly the egg-seller’s number: leaving 1 on dividing by 2, 3, 4, 5 and 6, and nothing on dividing by 7. He gave a surprising method: multiply the divisors 1 × 2 × 3 × 4 × 5 × 6 and add one. That is 721, which is 7 × 103. It isn’t the smallest answer (301 is), but it works, and not by luck: a theorem now named after John Wilson says that for any prime p, 1 × 2 × ⋯ × (p − 1) + 1 divides exactly by p. Try it with 5: 1 × 2 × 3 × 4 + 1 = 25.

A story from Sunzi to Gauss

Where this is used

  • Computing with very big numbers: exact calculations with large whole numbers are often done with remainders on dividing by several small numbers, each small enough for the computer’s own arithmetic, and the answer put together at the end (multi-modular computation).

Questions

★ Children in a school assembly line up in rows of 3, then of 4, then of 5, and each time one child is left over. There are fewer than 100 children and more than 40. How many?

Pick an answer to see why.

a.★

Han Xin’s legend: soldiers lined up in threes leave 2, in fives leave 3, in sevens leave 2. There were between 1,000 and 1,100 of them. How many?

Hint
Answer
b.★★

Using Sunzi’s numbers, find the number from 0 to 104 that leaves 0 in threes, 0 in fives and 3 in sevens. Then find one that leaves 1, 1 and 1. Why is the second obvious?

Answer
c.★★

A number leaves 2 on dividing by 4 and 3 on dividing by 6. Find all such numbers below 50, or explain why there are none.

Answer

★★ In which year AD will the next male Iron-Horse year of the Tibetan calendar begin? (2026 is a male Fire-Horse year; the elements go wood, fire, earth, iron, water, each for two years.)

Pick an answer to see why.

d.★★★

Find the smallest number that leaves 1 on dividing by each of 2, 3, 4, 5, 6, 7, 8, 9 and 10, and divides exactly by 11. Then check Ibn al-Haytham’s answer for the same puzzle, 1 × 2 × 3 × … × 10 + 1.

Hint
Answer

Where to go next

Where these facts come from
  • Sunzi’s problem (chapter 3, problem 26), his recipe with 70, 21 and 15 and “subtract multiples of 105”, the answer 23, the arguments over the book’s date, nothing known of Sunzi: MacTutor, “Sun Zi”, read October 2026. The problem retold in our own words.
  • The book’s three chapters, its place among the Ten Computational Canons, the facsimile page: Wikipedia, “Sunzi Suanjing”, read October 2026.
  • The theorem’s statement for pairwise coprime divisors, the uniqueness and counting proofs, Aryabhata’s algorithm, Brahmagupta’s special cases, Fibonacci’s Liber Abaci, Qin Jiushao’s 1247 general method, Gauss’s 1801 Disquisitiones with its calendar example, multi-modular computation, RSA’s use: Wikipedia, “Chinese remainder theorem”, read October 2026.
  • Qin Jiushao (1202–1261), the Shushu Jiuzhang written in mourning in 1244–1247, his corruption and the accusations of poisoning, Libbrecht’s “even more advanced than Gauss’s method”: MacTutor, “Qin Jiushao” and Wikipedia, “Qin Jiushao”, read October 2026.
  • Ibn al-Haytham (about 965–1040), his problem of 2, 3, 4, 5, 6 leaving 1 and 7 leaving none, his “canonical method” of multiplying the divisors and adding one, (7 − 1)! + 1 and Wilson’s theorem: MacTutor, “Ibn al-Haytham”, read October 2026.
  • Bhaskara II’s example (§ 160 of the Bijaganita, chapter on multiliteral equations), the answer 59 and the commentator’s 119: H. T. Colebrooke, Algebra, with Arithmetic and Mensuration, from the Sanscrit of Brahmegupta and Bháscara (1817, public domain), pp. 235–236, Internet Archive, read October 2026.
  • The Han Xin legend, “according to common folklore”: Wikipedia, “Han Xin”, read October 2026.
  • The Tibetan year cycle (five elements, each two years male then female, twelve animals, 60 years; 2026 male Fire-Horse; the rabjung cycle from 1027; Losar): Wikipedia, “Tibetan calendar”, read October 2026. Ten stems and twelve branches, the cycle ending at their LCM with half the 120 pairs unused: Wikipedia, “Sexagenary cycle”, read October 2026.
  • The Julian period of 28 × 19 × 15 = 7,980 years, Scaliger (1583), year 1 in 4713 BCE, de Billy’s multipliers 6,916, 4,200 and 4,845, the worked example for 2015, Julian days: Wikipedia, “Julian day”, read October 2026.
  • RSA decryption with dP d_P , dQ d_Q and qinv q_{\text{inv}} as part of the private key: RFC 8017, PKCS #1 v2.2, sections 3.2 and 5.1.2, read October 2026; the libraries that use it and the toy example with 61, 53 and the message 65: Wikipedia, “RSA cryptosystem”, read October 2026.
  • Every number on this page (the eggs’ 301 and its step 420, the tables, Sunzi’s 23 and blocks, the 3-4-5 and 2-3-5 tricks, Bhaskara’s 59 and 119, 2,519, 2,521 and 25,201, Wilson’s 721 and 3,628,801, the grids’ counts, the Tibetan years, the Julian period’s numbers for 2015 and 2026, the RSA toy) was computed by a script (checks/textbooks/math-further-9-12/08/tmp/answers.py).