Place a queen in each successive column of an infinite chessboard, always in the lowest row where no earlier queen attacks it. The paper Greedy Queens and the Golden Ratio proves that the queen in column n lies within a bounded distance of row nφ or of row n/φ, where φ = (1 + √5)/2 is the golden ratio (Theorem 1), and that the narrower ranges Knuth observed for these distances hold in every column (Theorem 2).
The proof reduces the theorem to a finite computation, and the paper also uses that computation to answer questions from the OEIS and to generate the sequence quickly with little memory. This repository contains all of that code, organised by the part of the paper it supports. Each folder has its own README with commands, expected output, and a map from files to sections of the paper.
The Lean 4 + mathlib formalization proves Theorems 1 and 2 and Corollaries 19 and 20 (diagonal coverage; column runs and gaps), including their finite certificates, using only kernel-checked proofs.
| Folder | Paper | What it contains |
|---|---|---|
formalization/ |
Sections 2–6, Appendix A | Lean proofs, kernel-checked finite certificates, and a paper-to-code guide |
verification/ |
Sections 4–6, Appendix A | The local calculation, the history graph, and the exhaustive checks behind Theorems 1 and 2 |
oeis/ |
Section 6.5, Appendix A | The finite graphs and witnesses behind the consequences for related OEIS sequences |
fast_generator/ |
Section 7, Appendix A | A C11 program that generates the sequence in linear time and logarithmic memory, with its measurements |
spire/ |
Beyond the paper | The Section 7 calculation reorganized for a modern processor: 1010 rows in 0.04 s on eight cores, in logarithmic memory; any stretch of rows written out; and single rows computed on their own, such as q10100 in 9 ms |
The Python programs need Python 3.10 or later and nothing else.
python verification/verify_tuples.py # the exhaustive check of Section 6.3 (a few seconds)
python verification/knuth_ranges.py # Theorem 2 and Knuth's ranges, Section 6.6 (a few seconds)
python verification/trace.py 41 44 # the actual local states, column by column (Section 4.6)
python verification/states.py 1000000 # the actual state before any column (Section 7.5)
python oeis/run_all.py # the checks behind Section 6.5 (under a minute)The generator needs a C11 compiler and make:
cd fast_generator
make
./build/queens_fast --count 1000000 # the first million rows, with a checksum
make verify && make test # its finite checks (about ten seconds)GitHub Actions runs these checks whenever the folders they cover change:
checks.yml runs every program of
verification/ and oeis/ on Python 3.10 and 3.13, and builds and tests the
generator with GCC, Clang, and Apple Clang, including the sanitizer builds and
the scanner of Knuth's ranges. Spire and the Lean formalization have workflows
of their own.
- Formal proofs in Lean. See
formalization/README.mdfor the theorem statements, paper correspondence, and build instructions. Run the Lean build commands fromformalization/. - Section 4 (the local state and the calculation). Run
verification/trace.pyon a few columns and compare its output with the examples of Section 4.6. Then readverification/calculation.py, which follows Section 4.5 and Algorithm 1 step by step. - Sections 5 and 6 (the proof). Run the commands in
verification/README.md.verify_tuples.pyis the finite check of Section 6.3, andknuth_ranges.pythe check of Section 6.6 behind Theorem 2;verify_bitmasks.pyandknuth_ranges_bitmasks.pyrepeat them independently. - Section 6.5 (OEIS consequences). See
oeis/README.md. - Section 7 (fast generation). See
fast_generator/README.md. For how far the same calculation can be pushed on a modern processor, seespire/README.md, which is not part of the paper.
The code is released under the MIT License; see LICENSE. The
comparison program fast_generator/knuth/knuth_packed.c contains code taken
from Donald Knuth's program infty-queens, which remains his; see
fast_generator/README.md.
The code uses the notation of the paper. A symbol of the queen word is 2u + b, where u = 1 when the column contains an upper queen (above the main diagonal) and b = 1 when the row does. A local state is the tuple (w, z, R, D, A, Hin, Q, Hout) of Section 4.2. Offsets are measured from the least unused row m.