There are 25 horses and a track with 5 lanes, so at most 5 horses can race at once. There is no stopwatch: a race tells you only the order the horses finished in, from first to last, with no ties. Each horse runs at the same speed every time, and the result of a race says nothing about how the horses would do against others. You want the three fastest horses, in order. How few races do you need?
Number the horses to . Put them in five groups of five, by number: horses 0 to 4, 5 to 9, and so on. A race returns the horses it holds sorted from fastest to slowest, and that is all the information there is. The whole game is deciding which races to hold and what each result lets you rule out.
Five races are not enough, because the five groups of five have been sorted within themselves, and nothing yet compares one group with another. A group's winner may be slower than the fifth-best horse of another group. After five races you know five orders and nothing about how they fit together.
This is the first plan, and it works.
That is 8 races. The script runs it for 100,000 random orderings of the 25 horses, with a seeded generator, and it finds the true top three, in order, every time, in exactly 8 races.
Lay the horses in a grid. Each group is a column, the fastest of the group at the top. Order the columns by the winners' race, so the column whose winner is fastest is on the left. Call the columns A to E, left to right, and the places in each column 1 to 5 from the top, so that A1 is the fastest horse of all, A2 is its group's runner-up, and E5 is the slowest horse of the slowest group.
What the six races have told you. Down every column the horses are in order, and along the top row the winners are in order, and nothing else is known. From those facts you can chain: if X is faster than Y and Y is faster than Z, then X is faster than Z. The script does this chaining for every horse in the grid, to count how many horses are provably faster than it.
For the horse in column and place , counting both from 0, the number is : the horses above it in its own column, and the winners of the columns to its left. The script's table, with columns A to E across and places 1 to 5 down, is:
| place | A | B | C | D | E |
|---|---|---|---|---|---|
| 1 | 0 | 1 | 2 | 3 | 4 |
| 2 | 1 | 2 | 3 | 4 | 5 |
| 3 | 2 | 3 | 4 | 5 | 6 |
| 4 | 3 | 4 | 5 | 6 | 7 |
| 5 | 4 | 5 | 6 | 7 | 8 |
Cross out everyone with three or more faster horses. A horse that three others are known to beat cannot be in the top three. That leaves the horses with 0, 1 or 2 known faster: 6 horses, which are A1, A2, A3, B1, B2, C1. The other 19 are out: A4, A5, B3, B4, B5, C2, C3, C4, C5, D1, D2, D3, D4, D5, E1, E2, E3, E4, E5.
Crown A1. A1 has nobody faster, so it is first, with no race needed. That leaves 5 horses for second and third: A2, A3, B1, B2 and C1. They fit exactly into one race. Run it, and the top two finishers are the second and third fastest overall.
So the second and third fastest are always among those five, wherever the true order puts them. The races are .
The script checks this on the same 100,000 random orderings: the grid plan finds the true top three, in order, every time, in 7 races.
The film uses one particular ordering of the horses in which the top three are spread across groups. The true top three are horses 10, 3, 11, in that order. Each line below is a race, with the finish order from fastest to slowest:
| race | the horses in the finish order |
|---|---|
| 1, group 0 to 4 | 3, 4, 0, 1, 2 |
| 2, group 5 to 9 | 5, 6, 7, 8, 9 |
| 3, group 10 to 14 | 10, 11, 12, 13, 14 |
| 4, group 15 to 19 | 15, 16, 17, 18, 19 |
| 5, group 20 to 24 | 20, 21, 22, 23, 24 |
| 6, the five winners | 10, 3, 15, 20, 5 |
The winners' race puts horse 10 first, so its group is column A, and the columns in order, from the left, begin with: 10, 11, 12, 13, 14 for A, 3, 4, 0, 1, 2 for B, 15, 16, 17, 18, 19 for C. The five horses left for second and third are the second and third places of A, the first and second of B, and the first of C: 11, 12, 3, 4, 15. The seventh race finishes 3, 11, 15, 4, 12. So the film's fastest horse is 10 and the next two are 3 and 11, which are the true top three, 10, 3, 11, in order. The plan that crowns them one at a time finds the same three: 10, 3, 11.
Is 7 the fewest? The page does not claim it. The script proves that 7 works, and that it works for every ordering it tries. It does not prove that 7 − 1 is impossible, so neither does the film. What the grid argument shows is that after six races, 19 horses are out of the running, and that the rest fit in one race.
Fewer places. If you needed only the single fastest horse, six races would do: five groups and the winners' race.
Other sizes. The method carries over to other numbers of horses and lanes in outline: sort the groups, race the winners, and cross out whoever already has as many faster horses as there are places you want. This page and its script only work out the 25-horse, 5-lane case.
scripts/twenty-five-horses.mjs asserts the claims below before it writes maths.json, and its --check fails if the file is stale. It checks that:
It does not search for a shorter plan. That 7 is the fewest is a separate question that nothing on this page settles.
D1 is slower than the winners of columns A, B and C, because the winners' race put D's winner fourth. That is three horses known to be faster, and a horse with three faster horses is at best fourth. In the table, D1 has 3 known faster.
A4 has A1, A2 and A3 above it in its own column, which is three known faster (3), so it cannot be in the top three. A3 has only A1 and A2 above it (2), so it still can.
Six: five groups, then the winners' race. The winner of the winners' race is the fastest overall.