---
title: "Mastermind in five guesses: Knuth's trick — The Daily Case"
description: "In 1976 Donald Knuth proved you can always crack Mastermind in five guesses. Follow one full game and see why the best guess is sometimes a wrong one."
url: "https://thedailycasegame.com/articles/mastermind-in-five-guesses"
site: "The Daily Case"
updated: "2026-11-25"
---

# Mastermind in five guesses: Knuth's trick

19 November 2026

![A wooden code-breaking board with rows of coloured pegs and a brass shield, beside a notepad of tally marks and a magnifying glass under a warm lamp](https://thedailycasegame.com/og/article-mastermind-in-five-guesses.jpg)

Four guesses, four replies, and exactly one secret code that fits them all. The colours are numbered 1 to 6. B means a right colour in the right place. W means a right colour in the wrong place.

- 1122: B
- 1344: W
- 3526: B W W
- 1462: B W

Can you find the code? Keep it in mind. The answer turns up below, and it is the best excuse for this article.

## 1,296 secrets

A standard game of Mastermind has four pegs and six colours, and colours can repeat. That makes 1,296 possible codes. Every guess gets one of 14 possible replies, from "nothing right" to "all four right".

The game came out in 1971–72. By Christmas 1975 it was enjoying a wave of popularity, and a Stanford computer scientist called Donald Knuth sat down and wrote a short paper about it for the Journal of Recreational Mathematics.

His aim was bold: to prove that the codebreaker can always win in five guesses or fewer, whatever the code. He also found room for a dry aside about the rules. The rule for white pegs is hard to state, he noted, and the makers had not managed it in their own instructions. A magazine had still given those instructions 8 points out of 8 for clarity.

## Ask the question with the smallest worst case

Knuth's method fits in two sentences. After every reply, work out which codes still fit everything you have been told. Then pick the guess whose worst possible reply leaves the fewest codes standing.

If two guesses tie, he takes one that could still be the answer. If that ties too, he takes the first in numeric order.

The opening move falls out of this. Knuth starts with two pairs, 1122, and whatever the reply, no more than 256 codes survive. Four different colours, 1234, looks cleverer and does worse: its worst reply leaves 312. That gap decides things. Start with 1123 and the method almost works, but one line of play fails to win in five. With 1234, Knuth thought a five-guess win looked impossible to guarantee.

## A game, move by move

The puzzle at the top is Knuth's own. He printed it near the end of his paper and hid the answer as a multiplication sum, 16 × 227.

Here is the game in full:

1. 1122 scores one B. Of the 1,296 codes, 256 still fit.
2. 1344 scores one W. Now 44 fit.
3. 3526 scores B W W. Seven fit.
4. 1462 scores B W. Only one code fits: 3632.

The fifth guess wins. If you got there with nothing but a pencil, well done.

## The brilliant throwaway

Look at that fourth guess again. Seven codes were still possible, and 1462 was not one of them. It could not win.

Knuth called it "really a brilliant stroke". He had checked, and none of the seven live codes would do the job. Guess any of them and two of the codes would still give the same reply. A guess that cannot win can be built to give all seven a different reply, so the fifth guess is certain.

That is the odd lesson of Mastermind. The best question is not always the likeliest answer. Sometimes you spend a whole turn on a guess you know is wrong, just to be sure of the next one.

## Faster on average, with a catch

Knuth did not claim the best possible average. He said his strategy was probably very close. It takes about 4.48 guesses per code. Later mathematicians shaved that down: Irving to 4.369, then Neuwirth to 4.364.

In 1993 Kenji Koyama, working in Kyoto, and Tony Lai, in Toronto, ran an exhaustive search on a computer and found the best average there is: 5,625 guesses added up over all 1,296 codes, or about 4.340.

The catch is that their strategy, which opens with 1123, needs a sixth guess in one unlucky corner of the game. They showed a small fix that holds it to five at almost no cost, an average of 4.341. They also pointed out that no strategy can promise fewer than five guesses in the worst case. So Knuth's five is still the best guarantee anyone can offer, and perfect play saves less than a seventh of a guess per game.

## What a person can borrow

Nobody memorises Knuth's strategy. His Figure 1 is a map of every possible game, a tree only four levels deep, written out in tightly packed brackets and four-digit codes. The habits behind it are easy to keep:

- Open with two pairs, not four different colours.
- After every reply, write down what still fits instead of guessing from a hunch.
- Judge a guess by how well it splits the list, not by how likely it is to be right.
- When few codes are left, look for a guess whose replies would all be different, even if it cannot win.

The same thinking runs through [seven habits for mystery and guessing games](https://thedailycasegame.com/articles/how-to-solve-mystery-games). Every Daily Case starts with a pile of possibilities and a few good questions.

[Play today's case](https://thedailycasegame.com/today)

## Sources

1. [Donald E. Knuth, "The Computer as Master Mind", Journal of Recreational Mathematics (1976–77)](https://www.cs.uni.edu/~wallingf/teaching/cs3530/resources/knuth-mastermind.pdf)
2. [Kenji Koyama and Tony W. Lai, "An Optimal Mastermind Strategy", Journal of Recreational Mathematics (1993), PDF copy](https://prodigious.blog/wp-content/uploads/2025/08/optimal-mastermind-strategy.pdf)
3. [Mastermind (board game), Wikipedia](https://en.wikipedia.org/wiki/Mastermind_%28board_game%29)
4. ["The Computer as Master Mind", notes on Knuth's solution tree, KTH Royal Institute of Technology](https://www.csc.kth.se/~weinkauf/notes/mastermind.html)

## More articles

- [Guess Who? strategy: the real odds](https://thedailycasegame.com/articles/guess-who-strategy-odds)
- [A short history of guessing games](https://thedailycasegame.com/articles/history-of-guessing-games)
- [How Cluedo was invented](https://thedailycasegame.com/articles/how-cluedo-was-invented)
- [The best mystery board games, ranked](https://thedailycasegame.com/articles/best-mystery-board-games)
- [How to get better at mystery and guessing games](https://thedailycasegame.com/articles/how-to-solve-mystery-games)
- [The rules of a fair mystery](https://thedailycasegame.com/articles/rules-of-a-fair-mystery)
- [All articles](https://thedailycasegame.com/articles)

```json
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "Mastermind in five guesses: Knuth's trick",
  "description": "In 1976 Donald Knuth proved you can always crack Mastermind in five guesses. Follow one full game and see why the best guess is sometimes a wrong one.",
  "datePublished": "2026-11-19",
  "inLanguage": "en",
  "publisher": {
    "@type": "Organization",
    "name": "The Daily Case"
  }
}
```
