An interactive companion

Greedy Queens and the Golden Ratio

Place a queen in each successive column of an infinite chessboard, always in the lowest row that no earlier queen attacks. The queens settle along two lines, of slopes and , and never stray far from them.

Scroll
1The greedy rulePaper §1–2

One queen per column, as low as possible

Number the rows and columns of a quarter-infinite chessboard , with the origin at the lower left. Put a queen at the origin. Then, in column , column , and so on, put a queen in the lowest square that no earlier queen attacks along a row, a diagonal or an antidiagonal.

This is OEIS A275895. Antti Karttunen introduced the positive-integer version, A065188, in 2001.Writing for the row of the queen in column , the rule produces Each earlier queen attacks at most three squares of a column, so some square is always free and every column gets a queen. Less obviously, every row eventually gets one too: Lemma 3 shows that is a permutation of .

Step through the rule below. In the highlighted column, a cross marks each square that an earlier queen attacks, and the gold square is the lowest one left. Hover overTap a cross, or a row number, to see the queen responsible.

The greedy rule, column by column. It starts at column , where all three kinds of attack occur; Figure 1 of the paper shows column , where every square below the queen lies in a row that is already taken. The color of a cross gives the line of attack: gray along a row, orange or teal along a diagonal below or above the line , indigo along an antidiagonal. Use the arrow keys to step, and the space bar to play. With sound on (the speaker at the top of the page), each attacked square ticks and each queen lands with a knock: bright for upper queens, warm for lower ones.

A queen is upper if it lies above the diagonal and lower if it lies below. Let the rule run for a while and a pattern appears: the upper queens climb steeply, the lower ones more gently, and both families seem to follow straight lines.

2Two golden linesPaper, Theorem 1

The queens never stray far from two lines

Here , and .Dekking, Shallit and Sloane conjectured that the upper and lower queens stay within a bounded distance of the lines and , where is the golden ratio. The paper proves it.

Theorem 1

For every ,

From far away the queens look like two straight rays, as in the picture at the top of this page. The figure below subtracts the rays: for each column it plots how far the queen lies above or below its line. Over a thousand columns or a million, the points fill two thin horizontal strips that do not widen.

The distance from each queen to its line, with a histogram of the distances on the right. Shaded: the bounds of Theorem 1. Gold: the sharper bounds of Proposition 21. Dashed: the ranges that Knuth observed for the first columns, which the paper's computation extends to . HoverTap to read off single columns.

Knuth states his ranges in -indexed coordinates, ; the figure converts them. See the remark after Proposition 8.The gold lines come from Proposition 21, which sharpens the constants of the theorem to Knuth's dashed ranges contain every point, but they are observations, not theorems. The gold lines are proved, and their upper ends lie at or below Knuth's, so the upper halves of Knuth's ranges hold in every column; for the lower queens the two upper ends coincide, at . Whether the lower halves hold in every column is still open.

The rest of this page follows the proof, which has two halves. A counting argument (Chapters 3 to 5) derives the golden lines from one bound on the lower queens. A finite computation and an induction (Chapters 6 to 9) prove that bound. The map below shows how the pieces fit; each box links to its chapter.

The proof at a glance.Chapters 10 and 11 are side trips: the queens as a game, and a fast way to generate them.

First, the upper queens, which are much simpler than the lower ones.

3Upper queensPaper §2

Upper queens take the diagonals in order

Number the upper queens from left to right. The upper diagonals are used in the same order.

Lemma 2

The th upper queen lies on the th upper diagonal: if it is in column , its row is .

The identity is implicit in Knuth's program infty-queens.The proof is an induction. Only upper queens lie on upper diagonals, and the first of them hold diagonals to , so the th upper diagonal is still free. When the th upper queen is placed in column , those diagonals block rows to of that column. Every earlier queen lies below and to the left of the square , so none of them shares its row or its antidiagonal, and the greedy rule takes it.

Write for the number of upper queens in columns to . An upper queen in column is the th, so it sits in row : the upper queens are determined by the single count . In the figure below, each curve joins an upper queen's column to its row. The th curve lands places to the right of where it starts, so the curves never cross.

Columns and rows of the upper queens (teal). The rows that no curve reaches are the lower rows. Move the slider to see Lemma 4: the th smallest lower row is . Hover overTap a curve to read it.

The rows that the curves miss are exactly the lower rows, and the figure shows why they are easy to count. Upper queens in columns before land below row , and the later ones land above it, so that row is the th lower row. This is a case of the Lambek–Moser theorem on complementary sequences.

The lower queens themselves are another matter. Their rows are not in increasing order, and nothing as simple as Lemma 2 places them.

4The heart of the proofPaper §3

One bound on the lower queens

Number the lower queens from left to right. The th lower queen, at , lies on the lower diagonal , and is its magnitude. If lower queens used the lower diagonals in order, as upper queens do, we would have . They do not, but they come close.

Lemma 5 (bounded diagonal discrepancy)

For every , .

The paper calls this bound the heart of its result. Chapter 5 shows how it forces the golden ratio, and Chapters 7 to 9 show how it is proved. In the figure, each strand runs from the th lower queen to its magnitude . Strands cross only near neighbors, and none travels more than four places.

The permutation of the lower queens, colored by : indigo below zero, orange above. Below, a strip of the values for the first lower queens (drag to move the window), and how often each value occurs among the lower queens in the first million columns.

The values and never occur. This is no accident of the first million columns: the checked states of Chapter 9 show that always, which the paper uses to sharpen its constants (Proposition 21). The same checked states also show that the least unused magnitude never falls more than four behind , so the magnitudes run through every positive integer; with Lemma 2, every diagonal of the board holds exactly one queen (Corollary 18).

5Why the golden ratioPaper, Proposition 8

How one bound forces the golden ratio

Let count the lower queens in columns to . Sorting the lower queens by row, with Lemma 4, turns the bound of Chapter 4 into an estimate for the columns of the lower queens (Lemma 6). From that estimate, Lemma 7 derives a relation between the counts and :

Why does this force ? Suppose that were exactly for some slope . Then , and Drag the slope (drag the gold number sideways, or focus it and use the arrow keys): with 0.600, the coefficient is 0.0400, which is positive. A bounded needs the coefficient to vanish, so and .

The paper makes this exact. Put . Substituting into the definition of , and using , gives The error at is a copy of the error at , shrunk by the factor , plus a kick of at most . Follow down to , where the error is . The kicks add up to less than

The error for up to a million (gray), with the chain (teal) for a chosen starting column; clicktap the plot to start elsewhere. Each hop divides by about . Shaded: the bound . Gold: the sharper bounds of Proposition 21. The table checks the identity above at every hop. When you choose a column, a light runs down the chain; with sound on, each landing is a note.

For an upper queen, exactly, because and . That is the upper half of Theorem 1. For a lower queen, differs from by , which Chapter 4 bounds by ; that is the lower half.

6The queen wordPaper, Definition 9

Four symbols per index

To prove the bound of Chapter 4, the paper describes the board by a word. For each index it records two bits: the column bit is when column holds an upper queen, and the row bit is when row does. The queen word is , with symbols

Column and row meet on the diagonal. Hover overTap the board or the word to choose . The strip under the board shows the column bits , and the strip beside it the row bits ; teal means an upper queen.

Before column , the symbols are already fixed: each column is filled, and an upper queen in row must lie in a column less than . The two bits are what the proof needs to know about upper queens. By Lemma 2, the upper queens are determined by counts of column bits, and a lower candidate square can be blocked by an upper queen in its row, which is a row bit.

The step in column uses the word in two places. It reads near the least unused row , where the lower candidates are, and it writes the new symbol . Since is about , the two places drift apart as grows. The proof stores only short pieces at each end, and near and just before , and has to cope without the long stretch in between.

The queen word before column , with the pieces that the local state keeps. Press play: the gap grows in proportion to , since tends to . The next two chapters explain the pieces and how the calculation manages without the gap.
7Eight recordsPaper §4–5

A calculation on eight records

The greedy step in column consults every earlier queen, and the board keeps growing. To prove a bound that holds in every column, the paper replaces the board by a local state (Definition 10): eight records, measured from three references that move as the queens are placed. They are the column , the least unused row , and the least unused lower-diagonal magnitude .

Lower candidates. Every row below is used, and so is every lower diagonal of magnitude less than . A lower queen in column must therefore go in a row from to , at an offset above , where . The sets , and record the rows, diagonals and antidiagonals of the earlier lower queens that could still attack these candidates, as offsets from , and . The records also give the discrepancy directly: if the new queen is the th lower queen, at offset , then So the heart of the proof reduces to bounds on two records. If and in every column, then .

Upper queens. Rather than store their positions, the state stores pieces of the queen word. The input history and the queue start near index and answer the candidates' row and antidiagonal tests. The output history holds the last twelve symbols before . One more number, , locates column among the upper queens, and this is all that Proposition 12 and Proposition 13 need.

The records before a column, and the calculation that produces the next queen and the next records (Algorithm 1). The board window shows the rows from up, with the lines of the recorded lower queens; the queen word runs underneath, column by column. ClickTap a step, or use the arrows, to see what it tests. The four presets are the worked examples of Section 4.6.

The calculation never uses , or . It reads only the records, so it can be run on any state, whether or not the state comes from a real board. Sometimes it needs a symbol beyond the end of , which the records do not hold: for the row count that gives the new row bit, for a candidate high above , for an upper queen whose antidiagonal must be checked, or for the search for the next unused row. It then makes a request to the history graph of Chapter 8, which lists the symbols that may come next. When the graph allows several, the calculation branches and follows each one. Column is an example.

Condition 15 and Lemma 16

Suppose the records before column satisfy that and , and that follows the history graph. Then the branch that answers every request with the actual symbol never stops, places the actual queen in column , and produces the actual records before column .

The figure above follows this actual branch. The bounds do two jobs. They keep every requested symbol within six places of , and together with they put at least eight places beyond , so every requested symbol is already determined. They also ensure that no unstored upper queen can affect a test. The condition holds from column on, since never decreases. What remains is to show that the bounds hold in every column. Since the calculation cannot tell which branch is the actual one, the paper checks them for every branch.

8The history graphPaper §4.3, §6.2

What may come next

The records keep the queen word only near index and just before , and the two ends drift apart: is before column and before column . So when the calculation needs a symbol past the end of , the records do not have it. The paper's answer is a finite history graph (Definition 11). Its vertices are words of twelve symbols, and an edge says that the symbol may follow the twelve symbols of ; the destination drops the oldest symbol and appends . A request is answered by the edges leaving the twelve symbols just read. At the other end, the output check confirms that the new symbol labels an edge leaving .

The figure places each vertex by its bits. Its column bits, newest first, give the horizontal coordinate as a binary fraction, and its row bits give the vertical one. The four quadrants hold the windows ending in , , and . Each quadrant splits in the same way by the symbol before, and so on. An edge appends a symbol, which halves both coordinates and moves the point toward the corner of the new symbol: it is one jump of the "chaos game".

The vertices and edges of the history graph. Teal: windows that the queen word passes through in its first million symbols, darker for more visits. Orange: windows the graph allows but the word never reaches there. Play the walk to follow the queen word through the graph, or replay the construction. Hover overTap a point to read its window.

The paper finds the graph by running the calculation itself (Algorithm 2). It starts from the eighteen windows of . Whenever a branch computes a symbol with no edge, it adds the edge instead of failing, and it repeats over all the states found so far until nothing new is added. Thirteen explorations add edges, and the fourteenth adds none. The construction only finds the graph. The proof holds the finished graph fixed and checks it.

The graph allows more than the queen word does. It has windows that never occur in and edges that never uses, because the state omits most of the board, so its records and the symbols it reads can combine in ways the board never produces. That is the price of a finite description, and the check in the next chapter has to cover all of these combinations.

9Checking every branchPaper §6.3–6.4

A finite check, and an induction

Start from the state before column , computed directly from the first thirty queens. Run the calculation on it, following every branch; run it on every successor; and continue until no new state appears (Algorithm 3). The check has no depth limit, and it never drops a branch.

Proposition 17 (finite verification)

With the history graph fixed, the calculation from the state before column reaches states and state-graph edges. Every branch passes the output check, and every successor satisfies Condition 15.

You can run the check yourself. The code behind it is a TypeScript port of the companion repository's verifier, and it reproduces the counts of Section 6.

The exhaustive check, run by this page. Each cell counts the reached states with a given and , filled in the order the exploration found them. The gold box is Condition 15; the check finds no state outside it.

An induction (Section 6.4) now follows the actual queens forever. Suppose that, before column , the actual records are one of the checked states and follows the history graph. Since from column on, Lemma 16 applies, and the actual branch carries out the actual step. By the check, its new symbol passes the output check, and its successor is again a checked state. Both assumptions therefore hold before column . At each lower queen from column on, the records give with and ; the lower queens before column are checked directly. This proves the bound of Chapter 4.

Why twelve symbols?

Shorter windows give a smaller graph, but it allows too much. The calculation can then read combinations of symbols that the board never produces, and some of them push the records out of bounds. Choose a length to rerun the construction in your browser.

The construction with histories of to symbols, as in the companion's history-length experiment (Appendix A2). Lengths to reach a state whose successor breaks Condition 15; lengths , and close.
10A game in disguisePaper, remark after Theorem 1

The queens as a game

The greedy queens also solve a game. A single queen stands on the board. Two players take turns moving it any number of squares to the left, down, diagonally down and to the left, or diagonally up and to the left, and the player who cannot move loses. Every move decreases , so the game always ends.

The losing positions, from which the player to move loses against best play, are exactly the greedy queens . No move joins two of them, because the queens do not attack one another. From any other square there is a move to one. If , move down to . If , the greedy rule passed over because an earlier queen attacks it, and that queen is one move away.

Play against an opponent who knows where the queens are. You move first, from a square that is not a queen, so you can always win: move to a greedy queen every time. Tick the box to see them.

Game theory gives every square a Sprague–Grundy value : the least nonnegative integer that is not the value of a square one move away. The losing squares are those with , the greedy queens, and Theorem 1 locates this zero set. The positive values are harder to describe. Every value occurs exactly once in each row and each column; Dekking, Shallit and Sloane conjectured that it also occurs exactly once on each diagonal, and Corollary 18 proves this for the value .

Sprague–Grundy values for columns to and rows to , darker for larger values. The squares with the chosen value are highlighted: gold for , the greedy queens, and orange otherwise. ClickTap a square to highlight its value.
11Little memoryPaper §7

A million queens from a few dozen records

The local description also gives a fast way to compute the queens. The calculation for column reads the queen word only up to about index , and . So the symbols it needs can be regenerated by a second copy of the same calculation, running behind the first. That copy needs symbols near , supplied by a third copy, and so on. Each copy receives the actual symbols and follows the actual branch, so nothing is guessed and the history graph is not needed.

When the first copy reaches column , the second is near , the third near , and so on until a copy is still within its first eighteen columns, so the chain has about copies: for a million queens. Their combined work is about queen placements, and no copy keeps more than a handful of small records.

A chain of copies, running in your browser. This is Algorithm 4, with symbols passed one at a time; each copy keeps only the records in the table, whatever the number of queens. Each arc runs from where a copy works to where it reads, which is where the next copy works (compare Figure 5).

The paper goes further. Whenever a copy pauses for input, its records take one of only values, so the calculation can be compiled into a table that places several queens per lookup (Section 7.3), and the copies pass symbols four at a time through buffers (Section 7.4). The C implementation in the companion repository generates ten billion queens in about seconds using MiB of memory. A bit-packed adaptation of Knuth's program takes about seconds and GiB.

12Further

Read further

The paper Greedy queens and the golden ratio The full proofs, the construction of the history graph, the consequences for OEIS sequences, the sharper constants, and the fast generator. The code github.com/boonsuan/queens The two independent verifiers of Section 6, the OEIS checks, and the C generator with its benchmarks. Each folder has its own README. The sequence OEIS A065188 and A275895 The greedy queens permutation, with the related sequences A275884–A275892, several of whose questions the paper settles.

Everything on this page is computed in your browser from the definitions. The queens come from the greedy rule. The explorer, the history graph and the check use a TypeScript port of the companion repository's verifier, which reproduces the counts of Section 6 and the positions of the copies in Figure 5 of the paper. The page needs no server and works offline.

About this companion

Designed and built by Claude Opus 5.5

This companion was designed, written and built by Claude Opus 5.5, an AI model made by Anthropic, at the request of the paper's author, Boon Suan Ho. Claude chose the chapters and the figures, wrote the prose, drew the visual design, and wrote all of the code: the widgets, and the TypeScript port of the verifier that runs the exhaustive check in your browser. The author gave the brief and reviewed the result.

The mathematics is the paper's. Every claim on this page was checked against it, and the port reproduces its numbers, from checked states to the positions of the copies in Figure 5. For the proofs, the paper is the authority.