WorksMathematics
Optimal single-track Gray codes from Gray-ordered necklaces
Abstract
A single-track Gray code of length is a cyclic Gray code of -bit words in which every column is a cyclic shift of the first. For prime the longest possible period is . Whether it is attained for every prime has been open since Hiltgen, Paterson and Brandestini introduced these codes in 1996, and optimal codes were known for primes . We show that a conjecture of Degni and Drisko (2007) on the reflected Gray code order of necklaces implies optimal single-track Gray codes for every prime. Their list of necklaces, conjecturally a Gray path for prime , closes into a Hamilton cycle of the necklace graph after explicit Pósa rotations, and such a cycle lifts to an optimal code. This implication is formalized in Lean 4. Together with computation it gives optimal single-track Gray codes for every odd prime : for explicit codes checked word by word by a program independent of the generator, and for , and codes obtained from the implication after the Degni-Drisko property was verified at these lengths by two independently written programs.
Introduction
A single-track Gray code of length and period is a cyclic sequence of distinct -bit words in which consecutive words (cyclically) differ in exactly one bit, and in which every column is a cyclic shift of column 0: there are head positions with for all and (indices mod ). Hiltgen, Paterson and Brandestini [6] introduced these codes for absolute rotary encoders. They showed that for such a code contains neither nor and that divides . For prime the period is therefore at most , and a code of period is called optimal. For a power of 2, Etzion and Paterson [2] constructed codes of period , and Schwartz and Etzion [12] showed that this is best possible.
For prime , optimal codes were known for . Mütze’s survey [9 Section 3.12] records that existence “has been tested and confirmed for all prime ” and lists the general question as open; Etzion [3] also lists the primes up to 19 as the known cases. Zhang and Zhu [13] list as the optimal period with evenly spread heads for all primes , derived from counting arguments (the number of aperiodic necklaces and a necessary condition on the sequence of weights); they report codes “with length up to 17” and print none. We know of no published optimal single-track Gray code for a prime .
Mütze [9 Section 3.12] describes the standard reduction. For prime every nonconstant necklace has distinct rotations, and an optimal code arises from a cyclic Gray code through the nonconstant necklaces, one representative each, in which the last representative is one flip away from a nontrivial rotation of the first. Lemma 1 shows that the nontriviality condition can always be met. The problem is therefore exactly the Hamiltonicity of the *necklace graph , whose vertices are the nonconstant necklaces of length and whose edges join necklaces having representatives at Hamming distance one.
Degni and Drisko [1] order necklaces by the first occurrence of a representative in the binary reflected Gray code (BRGC). They conjectured, and verified for , that consecutive necklaces in this order differ in a single bit except immediately after powers of odd primes (in their sense). For prime this says that their list is a Gray path through all necklaces, from to ; this is the prime case of Savage’s question whether necklaces of odd length admit a Gray code [11], [9 Problem P17]. Without its constant ends the path runs from to , which are not adjacent for . We close it explicitly.
Theorem A. Let be prime. If the Degni–Drisko list of necklaces of length is a Gray path, then has a Hamilton cycle, obtained from the list by Pósa rotations at explicit pivots, and an optimal single-track Gray code of length and period exists.
Theorem B. Optimal single-track Gray codes exist for every odd prime . For the codes are given explicitly and each has been checked word by word by a verifier independent of the generator (for , Lean’s kernel); for , and the codes are the ones produced by Theorem A, and the hypothesis of Theorem A was verified at these lengths by two independently written programs.
Theorem A is formalized in Lean 4 with Mathlib (ancillary files). The Lean theorem stgc_of_DD states that for every prime the Degni–Drisko property at , an explicit hypothesis, implies the existence of a single-track Gray code of length and period ; its proof uses only Lean’s standard axioms. The hypothesis there, adjacency in of consecutive necklaces of , is Conjecture 1 below. It is implied by the conjecture as stated in [1], which asks that consecutive first-occurrence words, constants included, differ in one bit. Lean proves the hypothesis itself only for , by computation. For it was verified by Degni and Drisko [1], and for , , and by the computations of § 4.
From Hamilton cycles in the necklace graph to codes
Words are , and is the left rotation; denotes the necklace (rotation class) of , and the word with a single in position .
Lemma 1. Let be prime. If has a Hamilton cycle, then there is an optimal single-track Gray code of length , with equally spaced heads.
Proof. Let be the cycle, . Choose and, inductively, bits with for ; finally choose with , say . If , put for , . Rotations are automorphisms of the cube, so consecutive words differ in one bit, also across the boundaries and cyclically since . The word lies in , and is injective because is invertible mod and has distinct rotations; so the words are exactly the nonconstant words. Finally for all , so column is column 0 delayed by , which is the single-track property with heads .
It remains to make . Rotate the cycle so that and take , a word with a single , in position ; then for some . Identify a word with the set of positions of its ones, so that shifts every position down by one (mod ), and put . Replace the lift by and for . Since contains , the first step is still a single flip; the later steps are single flips because is a cube automorphism; for all ; and the closing step now ends at . So the new shift is , and since , one of and is nonzero.
The Degni–Drisko list and its closing
The BRGC lists , , as -bit words with the most significant bit first. Its inverse is the prefix-parity map: has binary digits . Hence for words of equal length, if and only if is lexicographically smaller than . The first occurrence of a necklace is its rotation of smallest rank. Let be the list of the nonconstant necklaces of length ordered by the ranks of their first occurrences. (Degni and Drisko list all necklaces; has rank 0 and is the only necklace whose first occurrence begins with 1, so is a contiguous part of their list.)
Conjecture 1 (Degni–Drisko [1], prime case). For prime , consecutive necklaces of are adjacent in .
Degni and Drisko verified their conjecture for [1], Section 3. For composite odd the adjacency fails, for example at , 15 and 21, as the exceptions in their conjecture allow.
Let be odd and . Define the words
and for
Thus . Write , which is even, with for and . A Pósa rotation of a path at a pivot adjacent to the end replaces by , whose new end is .
Lemma 2. For odd the first necklace of is and the last is .
Proof. The first claim holds because . The first occurrence of is itself (rotations beginning with 1 have larger rank), with . Let be a nonconstant necklace; it has at least two zeros. Let () be the lengths of the cyclic runs of ones between consecutive zeros. If all were odd, then , which is false; so some is even. The rotation has prefix parities equal to those of up to position , where both are 0; at position the parity of stays 0 while that of becomes 1. So , and the first occurrence of precedes that of .
Lemma 3. and are adjacent in for .
Proof. Flipping position of gives . For , the end before step is (a rotation of) ; its rotation becomes when its second bit is flipped. (For this word is a rotation of as defined above.)
Lemma 4. is the first occurrence of its necklace.
Proof. The prefix parity begins with exactly as many zeros as . For , begins with and ends with 1, its cyclic runs of zeros have lengths and 1, and ; so is the only rotation whose prefix parity begins with zeros, and it has the smallest rank. For write with even. Rotations beginning with 1 are larger, and the only other rotation beginning with 0 is . The prefix parities of and agree in positions 1–3; if , position 4 gives 0 for and 1 for , and if they agree in position 4 and position 5 gives 0 for and 1 for .
Lemma 5. , and is the first occurrence of its necklace. Hence immediately follows in .
Proof. The words and differ exactly in the position of the lowest set bit of . The word ends in an even block of ones preceded by a zero (for the block , entered with parity 1; for the block 111, entered with parity 0), so ends in 01; thus , the lowest set bit of is in position , and flipping position of gives . For , begins with , ends with 1, and its other runs of zeros have length 1 or 2, so the argument of Lemma 4 applies. For , the rotations of beginning with 0 are , and , whose prefix parities begin 010, 011 and 011. Since is the BRGC successor of and is a first occurrence, no necklace has its first occurrence strictly between them.
Lemma 6. Let be the position of in . Starting from and performing the Pósa rotations at in turn is legal, and after the rotation at the end of the path is .
Proof. By Lemma 5, is at position . For : comparing prefix parities, (the words agree up to position , and at position the parity is 0 for and 1 for ), so is strictly before the end, is neither the end nor its predecessor, and by Lemma 3 the rotation is legal with new end . For : begins with exactly zeros and with , so , and since is ordered by rank, . After the rotation at the path is followed by reversed material, with end ; both and lie in the untouched prefix, at consecutive positions, so the rotation at (legal by Lemma 3) has new end .
Proof of Theorem A. By Lemma 2 and the hypothesis, is a Hamilton path of from to . By Lemma 6 the rotations produce a Hamilton path from (the start never moves) to , which is adjacent to . This is a Hamilton cycle; apply Lemma 1.
Computations
Codes for by search. We found Hamilton cycles in for all primes by Pósa rotation and extension [10], with Warnsdorff extension and a closing look-ahead, started from the Degni–Drisko list or from a greedy path. The search took under 2 seconds for , 3.5 and 12 minutes for two codes at , and 17 minutes and 20.6 GB of memory for . Each code was expanded to its full period and checked by at least two independent programs among a C bitset checker, a NumPy checker that reads only the track and the head positions, and, for , 29 and 31, two further independently written checkers.
Codes from Theorem A. The construction of Theorem A is algorithmic: the first occurrences can be enumerated in rank order with work each, and the cycle is a concatenation of segments of , traversed forwards or backwards. Our implementation enumerates in parallel, checks Conjecture 1 along the way, assembles the cycle, and lifts it to a flip sequence of length together with the shift . For the resulting codes pass the C bitset checker, the NumPy checker and the flip-sequence verifier described next. For the code has and ; it was produced in 15 minutes on four cores. A separately written verifier regenerated all words from the flip sequence, marking them in a -bit table, and confirmed that every nonconstant word occurs exactly once, that the passes chain with shift 2, and that the sequence closes.
The Degni–Drisko property at , , . By Theorem A, a code for prime follows from Conjecture 1 at . We verified it by streaming the BRGC ranks with constant memory per thread. At , has 53,634,713,550 necklaces, every consecutive pair is adjacent, and the list runs from to (77 minutes on four cores). A second program, written independently without access to the first (it computes the ranks of all rotations incrementally and scans only ranks below ), confirmed the same count, the same first and last necklaces and no non-adjacent pair (36 minutes on four cores); it also confirmed . At (204,560,302,842 necklaces) both programs found every consecutive pair adjacent (4.7 and 3.0 hours on four cores, respectively). At (2,994,414,645,858 necklaces) both programs, with checkpointing, again found every consecutive pair adjacent and the expected first and last necklaces (64 and 55 hours of wall time, respectively, on four cores of a shared laptop).
| period | method | verification | |
| 3 | 6 | explicit | Lean (decide) |
| 5–19 | search; Theorem A () | word by word, 2 checkers | |
| 23 | 8,388,606 | search (, ) | word by word, checkers |
| 29 | 536,870,910 | search (, ) | word by word, checkers |
| 31 | 2,147,483,646 | search () | word by word, checkers |
| 37 | 137,438,953,470 | Theorem A () | word by word, 1 checker |
| 41 | 2,199,023,255,550 | Theorem A | Conjecture 1 at , 2 programs |
| 43 | 8,796,093,022,206 | Theorem A | Conjecture 1 at , 2 programs |
| 47 | 140,737,488,355,326 | Theorem A | Conjecture 1 at , 2 programs |
Table 1. Optimal single-track Gray codes. “Word by word” means that a program written separately from the generator regenerated every word of the period. For , , the codes are the explicit ones of Theorem A; they were not expanded.
Further observations and open problems
Symmetric chains. Griggs, Killian and Savage [4] (see also [7]) gave, for prime , a rotation-invariant symmetric chain decomposition of the Boolean lattice; it induces a decomposition of into paths. Gregor, Mička and Mütze [5] proved that the Greene–Kleitman decomposition of the cube extends to a Hamilton cycle and conjectured that every symmetric chain decomposition does.
Conjecture 2. For every prime the Griggs–Killian–Savage chains (with and removed) extend to a Hamilton cycle of .
We found such cycles for every prime by a search on the chains as two-ended super-vertices. Every chain has an odd number of edges, so chain bottoms are joined only to bottoms and tops only to tops, apart from joins between chains of the middle two levels; these cycles are “cycle orderings” in the sense of [5].
Central levels. Merino, Mička and Mütze [8] showed that the middle two levels of the cube have a Hamilton cycle invariant under rotation, for every odd ; in particular, the middle two levels of the necklace graph are Hamiltonian, for prime and composite odd .
Conjecture 3. For every odd and every , the subgraph of the necklace graph induced by the necklaces of weight in is Hamiltonian.
The case is the theorem of [8], and the case , prime, is equivalent (by Lemma 1) to the existence of optimal single-track Gray codes. Our search found such Hamilton cycles, checked by the search program itself, for all odd and all . For and , , , , (periodic necklaces included for composite ) the cycles also passed an independent checker.
Structure of the Degni–Drisko conjecture. Degni and Drisko prove that the strings skipped between consecutive Gray necklaces are covered by their “symmetric intervals”, note that removing a single such interval preserves Hamming distance one, and identify several intervals as the difficulty [1 Section 8]. For prime every gap between consecutive necklaces of is a single symmetric interval. This fails at and : in all 7 and 21 exceptional gaps, respectively, the gap consists of exactly three symmetric intervals, a central one flanked by two whose generators have equal length, between and .
Ancillary files
The arXiv ancillary files contain the Lean 4 project (lean/, with toolchain and Mathlib pins), the programs for the search, the construction of Theorem A and the verifiers, the tracks and head positions of the search codes for (the tracks for and , 0.5 and 2.1 GB uncompressed, exceed arXiv’s size limit and are available at https://github.com/dzoba/single-track-gray-codes), and the logs of the Degni–Drisko verifications at , , and and of the checks described in § 4. The flip sequence (3.7 GB, SHA-256 0a0c91ea19527ee4812ff2b0d8627d3abe6e0d4fbaad611a48efbb13f8c5b811) can be regenerated by the included construction program.
Use of AI tools
This work was carried out with AI systems under the author’s direction. Claude (Anthropic) wrote and ran the searches and verifiers, found the closing pivots of Theorem A by computer search, wrote the proofs and the Lean formalization, and drafted the text. The word-by-word verifiers share no code with the generators, and the second Degni–Drisko checker was written by a separate AI instance from a specification only. Separate instances refereed the proof and the formalization, and Codex (OpenAI) wrote two further checkers and checked the codes for , 29 and 31. The author is responsible for the content.
References
- [1]C. Degni and A. A. Drisko, Gray-ordered binary necklaces, Electron. J. Combin. 14 (2007), #R7.DOI
- [2]T. Etzion and K. G. Paterson, Near optimal single-track Gray codes, IEEE Trans. Inform. Theory 42 (1996), 779–789.DOI
- [3]T. Etzion, Binary and non-binary self-dual sequences and maximum period single-track Gray codes, arXiv:2512.09655 (2025).arxiv.org/abs/2512.09655
- [4]J. Griggs, C. E. Killian and C. D. Savage, Venn diagrams and symmetric chain decompositions in the Boolean lattice, Electron. J. Combin. 11 (2004), #R2.DOI
- [5]P. Gregor, O. Mička and T. Mütze, On the central levels problem, J. Combin. Theory Ser. B 160 (2023), 163–205.arxiv.org/abs/1912.01566
- [6]A. P. Hiltgen, K. G. Paterson and M. Brandestini, Single-track Gray codes, IEEE Trans. Inform. Theory 42 (1996), 1555–1561.
- [7]C. E. Killian, F. Ruskey, C. D. Savage and M. Weston, Half-simple symmetric Venn diagrams, Electron. J. Combin. 11 (2004), #R86.DOI
- [8]A. Merino, O. Mička and T. Mütze, On a combinatorial generation problem of Knuth, SIAM J. Comput. 51 (2022), 379–423.
- [9]T. Mütze, Combinatorial Gray codes—an updated survey, Electron. J. Combin. (2023), Dynamic Survey DS26.arxiv.org/abs/2202.01280
- [10]L. Pósa, Hamiltonian circuits in random graphs, Discrete Math. 14 (1976), 359–364.DOI
- [11]C. D. Savage, A survey of combinatorial Gray codes, SIAM Rev. 39 (1997), 605–629.DOI
- [12]M. Schwartz and T. Etzion, The structure of single-track Gray codes, IEEE Trans. Inform. Theory 45 (1999), 2383–2396.DOI
- [13]F. Zhang and H.-j. Zhu, Determination of optimal period of absolute encoders with single track cyclic Gray code, J. Cent. South Univ. Technol. 15 (Suppl. 2) (2008), 362–366.DOI