Number

One card from every pile

Split a deck into piles, take one card from every pile, and put the cards taken into a new pile. Repeat. With ten cards, every starting arrangement ends at piles of 4, 3, 2 and 1, which the move leaves unchanged, and the slowest takes twelve moves; with fifteen it is 5, 4, 3, 2, 1 after at most twenty. The move is a partition turning its first column into a row, the bound is exactly k² − k for k(k + 1)/2 cards, and for any other number of cards the game never stops — it goes round in cycles, and the cycles are necklaces.

Worth reading first: A diagram turned on its side · Necklaces that prove a theorem.

Deal fifteen cards into piles of any sizes. Now take one card from every pile, put the cards taken together as a new pile, and set the old piles, each one card smaller, beside it; a pile that has run out simply disappears. Do it again, and again. Whatever the starting arrangement, after at most twenty moves the piles have sizes 5, 4, 3, 2 and 1, and from then on nothing changes, because taking a card from each of five piles makes a new pile of five and leaves 4, 3, 2, 1 and nothing.

The game is called Bulgarian solitaire, and it reached a wide audience through Martin Gardner’s column in 1983, which posed the question of why fifteen cards always settle and what happens with other numbers. The answers are a small, complete piece of mathematics. Fifteen is a triangular number, 1+2+3+4+51 + 2 + 3 + 4 + 5, and for every triangular number the staircase is the only end; the longest possible approach is exactly k2−kk^2 - k moves for k(k+1)/2k(k + 1)/2 cards. For any other number of cards the game does not end at all. It falls into a cycle, and the cycles have a description that is pure combinatorics: they are the ways of placing a few beads on a necklace.

The game is a map from a finite set to itself, the setting of Kaprekar’s routine, so every orbit must eventually repeat; the content is in which repetitions occur. Every number in this essay comes from following every arrangement of every number of cards up to 45 — 89,134 starting positions for 45 cards alone — until it repeats. The arguments that explain the numbers are older than the computation; the computation shows they are right where they can be checked, and draws what they say.

A column becomes a row

A pile of 10 cards reaches the staircase. 10 | 9,1 | 8,2 | 7,2,1 | 6,3,1 | 5,3,2 | 4,3,2,1
Fig. 1 Bulgarian solitaire from a single pile of ten. Each move takes the shaded first column — one card from every pile — and stands it up as a new row. After six moves the piles are 4, 3, 2, 1.

An arrangement of nn cards into piles, with the order of the piles ignored, is a partition of nn — the objects that every partition, hidden in a product counted by a generating function — and the natural picture of a partition is its Ferrers diagram: one row of cells for each pile, longest at the top. A diagram turned on its side used that picture to read columns as well as rows, which is the whole of what conjugation is, and the solitaire move is half a conjugation. Taking one card from every pile removes exactly the first column of the diagram, a column as long as the number of piles. Putting those cards into a new pile makes a row of that length. The move is: take the first column, lay it down as a row, and sort.

The figure starts with all ten cards in one pile. The first column is a single cell, which becomes a pile of one: 9 and 1. The first column now has two cells, so the next pile has two: 8 and 2. Then 7, 2 and 1; 6, 3 and 1; 5, 3 and 2; and then 4, 3, 2 and 1, in six moves. The staircase has first column 4, the length of its longest row, so laying it down as a row recreates the row that was taken away and the diagram is unchanged.

That is the whole reason a staircase is fixed: its first column and its first row are the same length, and the rows below the first are exactly the rows of the diagram with its first column removed. A diagram fixed by the move must have that property at every level, which forces the rows to fall by exactly one at a time, so the staircase is the only fixed shape — and a staircase exists only when nn is triangular.

The slowest start

How far every partition starts from the staircase. 10: 42 partitions, max 12, worst 3,3,2,1,1, median 6; 15: 176 partitions, max 20, worst 4,4,3,2,1,1, median 13; 21: 792 partitions, max 30, worst 5,5,4,3,2,1,1, median 23; 28: 3718 partitions, max 42, worst 6,6,5,4,3,2,1,1, median 35; 36: 17977 partitions, max 56, worst 7,7,6,5,4,3,2,1,1, median 48
Fig. 2 For each triangular number from 10 to 36, every partition sorted by how many moves it needs to reach the staircase. The longest approach, marked, is exactly k2−kk^2 - k; the counts pile up against it.

That every partition of a triangular number reaches the staircase is not obvious, because the move can make some arrangements temporarily worse — further from the staircase in any obvious sense — before they improve. Jørgen Brandt proved in 1982 that the staircase is always reached, and the question then became how long it can take. Kiyoshi Igusa and, independently, Gérard Etienne proved that for k(k+1)/2k(k + 1)/2 cards the staircase is reached within k2−kk^2 - k moves.

The figure checks the bound against every partition of 10, 15, 21, 28 and 36 cards. For each, the partitions are sorted into a histogram by the number of moves they need. The longest approach is exactly 12, 20, 30, 42 and 56 moves — k2−kk^2 - k for k=4,5,6,7,8k = 4, 5, 6, 7, 8 — so the bound is not only true but attained. The slowest start in each case has a recognisable shape: for 36 cards it is 7, 7, 6, 5, 4, 3, 2, 1, 1, which is the staircase 8, 7, …, 1 with the top cell of its first row moved to the bottom of its first column. That small disturbance takes 56 moves to repair.

The shape of the histograms is the less expected part. Most partitions are not close to the staircase: the counts are lowest near zero and highest near the maximum, so that half of the 17,977 partitions of 36 need 48 moves or more out of a possible 56. A random arrangement of cards is, in this game, nearly as slow as the worst. The explanation, described below, is that most of the approach is spent going round a ring of positions that is almost a cycle, and almost every partition has to go nearly all the way round.

Why it takes so long

The argument behind the bound is worth seeing, because it explains the histograms as well. Draw the Ferrers diagram with its cells labelled by their diagonals: the corner cell is on diagonal 1, the two cells next to it on diagonal 2, and so on, the cells with row index ii and column index jj lying on diagonal i+j−1i + j - 1. The staircase for k(k+1)/2k(k+1)/2 cards fills diagonals 1 to kk exactly. A partition that is not the staircase has some cells beyond diagonal kk and some holes on or below it.

The move, viewed on diagonals, is almost a rotation. Removing the first column and standing it up as a row shifts every other cell one place along its diagonal — a cell at row ii, column jj moves to row i+1i + 1, column j−1j - 1, which is the same diagonal — and puts the old first column, cell by cell, onto the first row, also keeping each cell on its diagonal. So without the sorting step, every cell stays on its own diagonal and simply travels round it: a diagonal with dd places is a cycle of length dd. Sorting the rows is the only thing that can move a cell from one diagonal to another, and it always lowers the sum of the diagonal numbers of all the cells: lifting a longer row above a shorter one moves each cell of the longer row one diagonal in and each cell of the shorter row one diagonal out, and there are more of the first.

So a partition approaches the staircase by having its outer cells carried round their diagonals until they line up with a hole on a smaller diagonal and drop into it. The worst case is a cell that has to travel nearly all the way round a long diagonal to reach its hole, many times over, and the count of those journeys is what gives k2−kk^2 - k. The histograms pile up near the maximum because a typical partition has outer cells scattered round the outermost diagonals, and the last of them to find its hole has, typically, nearly a full circuit to make.

Between the triangular numbers

The longest game of Bulgarian solitaire. 3:2 4:2 5:3 6:6 7:4 8:5 9:7 10:12 11:8 12:8 13:9 14:14 15:20 16:15 17:12 18:13 19:16 20:23 21:30 22:24 23:18 24:18 25:19 26:26 27:34 28:42 29:35 30:28 31:24 32:25 33:29 34:38 35:47 36:56 37:48 38:40 39:32 40:32 41:33 42:42 43:52 44:62 45:72
Fig. 3 The most moves any partition of n cards needs to reach its cycle, for every n from 3 to 45. At the triangular numbers it is exactly k2−kk^2 - k; between them it dips, then climbs past the next bound one card short of the next triangular number.

When nn is not triangular, the staircase cannot be reached, and the orbit of every partition ends in a cycle instead. The time to reach a cycle is still finite, and the figure plots the longest such time for every nn from 3 to 45. At the triangular numbers, the marked points, it is the bound k2−kk^2 - k. Immediately after one, it falls sharply: 30 moves at 21 cards, but only 24 at 22 and 18 at 23. With one card more than the staircase, the cycle is a ring of k+1k + 1 positions and a partition can join it at any of them, rather than having to arrive at a single point, so arriving is easier.

Then the curve climbs again, and it overshoots. One card short of each triangular number, the longest approach exceeds the bound for the triangular number below: 14 at 14 cards against 12 at 10, 23 at 20 against 20 at 15, 34 at 27 against 30, 47 at 35 against 42, 62 at 44 against 56. With kk extra cards on a diagonal of k+1k + 1 places there is again only one cycle — every place filled but one — and the configuration has nearly the rigidity of a triangular number with one more diagonal to fill.

For these other values of nn no exact formula for the longest approach is known in general. The triangular numbers are the case the bound covers; between them the figure is a measurement, and its zigzag is the shape a formula would have to reproduce.

Twelve cards go round

The cycles of 12 cards are necklaces. 5,4,2,1 > 4,4,3,1 > 4,3,3,2 > 4,3,2,2,1 > 5,3,2,1,1 || 5,3,3,1 > 4,4,2,2 > 4,3,3,1,1 > 5,3,2,2 > 4,4,2,1,1
Fig. 4 Both cycles of twelve cards. Every partition on a cycle is the staircase 4, 3, 2, 1 plus two cells on its next diagonal, read as a word of five places beneath each diagram; each move turns the word one place.

Twelve cards is the staircase of ten plus two. Following every one of the 77 partitions of 12 finds that every orbit ends in one of two cycles, each of length five. The figure draws both, and the description is visible in the drawing: every partition on either cycle is the staircase 4, 3, 2, 1 with its two extra cards placed on the fifth diagonal, the five places just outside the staircase, at most one to a row.

Read the fifth diagonal as a word of five letters, 1 where there is an extra card and 0 where there is not. On the first cycle the word for 5, 4, 2, 1 is 11000 — extra cells in the first two rows — and the next partition, 4, 4, 3, 1, has the word 01100. Each move rotates the word one place, which is exactly the rotation of cells along their diagonal described above, with nothing to drop inward because there is no hole left inside the staircase. After five moves the word has gone round and the partition is back. The second cycle has the word 10100, two extra cells separated by a gap, and rotating it also takes five moves to return.

So the two cycles of twelve cards are the two genuinely different ways of placing two beads on a ring of five places: adjacent, or one apart. A word and its rotations are the same cycle, which is what makes it a necklace rather than a word. The surprise is that the dynamics, which looked like a matter of piles and cards, has reduced to the combinatorics of the necklaces that prove a theorem — the same objects whose count gives Fermat’s little theorem.

Every cycle is a necklace

Cycles of Bulgarian solitaire counted as necklaces. 1=T1+0:1 2=T1+1:1 3=T2+0:1 4=T2+1:1 5=T2+2:1 6=T3+0:1 7=T3+1:1 8=T3+2:2 9=T3+3:1 10=T4+0:1 11=T4+1:1 12=T4+2:2 13=T4+3:2 14=T4+4:1 15=T5+0:1 16=T5+1:1 17=T5+2:3 18=T5+3:4 19=T5+4:3 20=T5+5:1 21=T6+0:1 22=T6+1:1 23=T6+2:3 24=T6+3:5 25=T6+4:5 26=T6+5:3 27=T6+6:1 28=T7+0:1 29=T7+1:1 30=T7+2:4 31=T7+3:7 32=T7+4:10 33=T7+5:7 34=T7+6:4 35=T7+7:1 36=T8+0:1 37=T8+1:1 38=T8+2:4 39=T8+3:10 40=T8+4:14 41=T8+5:14 42=T8+6:10 43=T8+7:4 44=T8+8:1
Fig. 5 For every n = k(k + 1)/2 + r up to 44, the number of distinct cycles Bulgarian solitaire ends in, found by following every partition. In every cell it equals the number of necklaces of r beads on k + 1 places.

The same structure holds for every number of cards. Write n=k(k+1)/2+rn = k(k+1)/2 + r with 0≤r≤k0 \le r \le k, so that the staircase of kk rows is the largest that fits. Every cycle consists of that staircase with rr extra cells on diagonal k+1k + 1, which has k+1k + 1 places, at most one cell to each, and the move rotates the pattern. So the cycles correspond exactly to the binary necklaces of length k+1k + 1 with rr ones, and the length of each cycle is the period of its necklace, which divides k+1k + 1.

The figure tests that statement against the computation for every nn up to 44, arranged as a triangle with kk down the side and rr across. In every one of its cells the number of cycles found by following all the partitions agrees with the number of necklaces. The triangle reads like Pascal’s triangle divided by rotations: 4 cycles at 38 cards, 10 at 39, 14 at 40 and 41, 10 at 42 and 4 at 43, against the binomial coefficients (9r)\binom{9}{r} divided by about nine. The division is not exact, because a necklace with a symmetry has fewer distinct rotations — the word 100100100 rotates back to itself after three places — and those are precisely the cycles shorter than k+1k + 1: the cycle of length 3 among the ten at 39 cards, and the cycles of length 4 and 2 among the ten at 32, from 11001100 and 10101010.

At r=0r = 0 and r=kr = k there is exactly one necklace, all empty or all full but one, so the game has a single fate. That is why 14, 20 and 27 cards behave like triangular numbers in having one cycle, and why they are the peaks of the previous figure: one fate, reached from everywhere, is the configuration that takes longest to reach.

A conservation law in disguise

There is a way to see why the outer cells must end up on a single diagonal, and it shows the game as a kind of physical system. Give each cell of a Ferrers diagram a weight equal to its diagonal number, and add up the weights. The move keeps every cell on its diagonal before sorting, so it leaves the total weight unchanged; sorting, as described above, strictly decreases the total whenever it does anything. The weight is therefore a quantity that never increases, which is the classic shape of a proof that a process settles: a potential energy that can only fall, the discrete cousin of the slope that decides whether a staircase of iterates closes in on a fixed point.

The weight is bounded below by its value on the most compact arrangement, which is the staircase plus extra cells on one more diagonal. Once the weight stops falling, sorting never moves anything again, and the move is a pure rotation of cells along their diagonals; a configuration where rotation never forces any re-sorting is one where every diagonal below the outermost is full. That is exactly the description of the cycles. Brandt’s proof and its successors make this argument exact, and the k2−kk^2 - k bound comes from counting how long the weight can stay at each value before it is forced to fall.

The genuinely surprising part is the way the two classical pictures of a partition meet. Ferrers diagrams are usually read by rows, for pile sizes, or by columns, for conjugation. The solitaire reads them by diagonals, the direction in which the shape a random partition takes is most naturally described, and along the diagonals a seemingly arbitrary card game becomes a rotation, and its fates become necklaces.

Where exhaustion stops and proof begins

Every figure here is an exhaustive computation, and exhaustion stops at a size. The bound k2−kk^2 - k is proved for all kk; the figure confirms that it is attained for kk up to 8, which is evidence that the proof is sharp but not, by itself, a proof of sharpness for larger kk — that comes from exhibiting the slowest partition, which the figure shows has the same shape in every case drawn. The necklace description of the cycles is likewise a theorem, and the census confirms it to 44 cards; beyond that the census is silent and the theorem is not.

The diagonal argument is described in words rather than drawn. The figures show where orbits begin and end, and what the cycles look like, but not the cells travelling round their diagonals, which is the mechanism. And they say nothing about the game played with any other rule — taking two cards from every pile, or one card from every pile of at least two, or choosing at random whether to take from each pile — for which the fates are different and in several cases not fully known.

Finally, the figures describe the game as a deterministic map. A real deck dealt at random into piles is a random partition, but not a uniformly random one, and the histogram of approach times for cards actually dealt would weight the partitions differently. The pile-up near the maximum is a statement about partitions counted equally.

Still open: the longest game between the triangles

For nn between triangular numbers the longest approach is the zigzag in the figure, and no formula for it is proved. Jerrold Griggs and Chih-Chang Ho studied the question in the 1990s, computed the maximum for many cases and conjectured a general formula; a proof that nothing does worse than their candidates is still missing. The figure’s overshoot one card short of each triangular number — 62 moves at 44 cards against 56 at 36 — is the part a formula would most need to explain.

There are questions about typical behaviour too. The histogram at 36 cards has its median at 48 of 56 moves; how the typical approach time grows with kk, as a fraction of k2−kk^2 - k, and whether that fraction tends to one, is a question about the shape of a random partition near the corner of its diagram, and the limit shape of random partitions is the obvious place to start. For a random rule, in which each pile gives up a card only with some probability, the game becomes a Markov chain on partitions, whose typical shapes have been studied in their own right; the deterministic game is the limit in which every pile always gives, and how the random game’s behaviour approaches it is not fully understood.

Columns laid down as rows

Bulgarian solitaire takes the first column of a partition’s Ferrers diagram and lays it down as a row. For k(k+1)/2k(k+1)/2 cards every partition reaches the staircase k,k−1,…,1k, k-1, \ldots, 1, which the move fixes because its first column equals its first row, and the longest approach is exactly k2−kk^2 - k moves — attained for every kk to 8 by the staircase with one corner cell moved, while half the partitions of 36 need at least 48 of the 56. For any other nn the game ends in cycles: the staircase plus rr cells on its next diagonal of k+1k+1 places, turned one place by each move, so that the cycles are binary necklaces of rr beads on k+1k+1 places and their lengths divide k+1k+1 — confirmed for every nn up to 44. Read along its diagonals, a card game is a rotation.