PuzzleReels
Sections

Twenty-Five Horses

The maths · Episode 31 · 2:38 · gives the answer

The puzzle

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?

The maths, properly

Number the horses 0 to 24. 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.

The wrong turn: five races

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.

Route one: crown them one at a time

This is the first plan, and it works.

  1. Races 1 to 5. Race each group. Now each group is sorted.
  2. Race 6. Race the five group winners. The winner of this race is the fastest horse of all, since every other horse is slower than its own group's winner. Call it A1.
  3. Race 7. The second fastest horse is either A1's runner-up in its group or one of the other four group winners. Race those five: A1's group runner-up, and the winners of the other four groups. The winner of this race is the second fastest overall.
  4. Race 8. The horse just crowned second has left its column's front, so move that column on by one place, and race the five horses now at the front of the columns. The winner is the third fastest overall.

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.

Route two: the grid, and crossing out

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 c and place r, counting both from 0, the number is c+r: 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:

placeABCDE
101234
212345
323456
434567
545678

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 5+1+1=7.

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.

A worked example: the film's field

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:

racethe horses in the finish order
1, group 0 to 43, 4, 0, 1, 2
2, group 5 to 95, 6, 7, 8, 9
3, group 10 to 1410, 11, 12, 13, 14
4, group 15 to 1915, 16, 17, 18, 19
5, group 20 to 2420, 21, 22, 23, 24
6, the five winners10, 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.

What changes if the numbers change

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.

Checking it

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:

  • for 100,000 seeded random orderings of the 25 horses, the route that crowns one at a time always takes 8 races and finds the true top three in order;
  • for the same orderings, the grid route always takes 7 races and finds the true top three in order;
  • after the first six races, the chaining of "faster than" gives exactly 6 horses with fewer than three known to be faster, which are A1, A2, A3, B1, B2, C1, and 19 with three or more;
  • A1 has none known faster;
  • in the film's field the final race has five horses, and both routes find the true top three 10, 3, 11.

It does not search for a shorter plan. That 7 is the fewest is a separate question that nothing on this page settles.

Where this shows up

  • Transitivity. Every race result is a set of "faster than" facts, and the useful work is chaining them. The same idea underlies sorting with few comparisons.
  • Partial orders. The grid is a picture of a partial order: some pairs are known and some are not. The question "who can still be in the top three" is a question about that order.
  • Tournaments. Real knockout tournaments and seeding systems are the same problem with the extra complication that results are noisy.

Try it yourself

1. After the six races, why can horse D1 not be in the top three?

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.

2. Why does the film's seventh race include A2 and A3 but not A4?

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.

3. How many races to find only the fastest horse?

Six: five groups, then the winners' race. The winner of the winners' race is the fastest overall.