arXiv:2610.24758math.COOct 2026

Manuscript Offprint

arXiv:2610.24758

Optimal single-track Gray codes from Gray-ordered necklaces

Chris Dzoba

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

A single-track Gray code of length nn is a cyclic Gray code of nn-bit words in which every column is a cyclic shift of the first. For prime nn the longest possible period is 2n−22^n - 2. 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 n≤19n \leq 19. 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 nn, closes into a Hamilton cycle of the necklace graph after (n−3)/2(n - 3)/2 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 n≤47n \leq 47: for n≤37n \leq 37 explicit codes checked word by word by a program independent of the generator, and for n=41n = 41, 43 and 47 codes obtained from the implication after the Degni–Drisko property was verified at these lengths by two independently written programs.

1 Introduction

A single-track Gray code of length nn and period PP is a cyclic sequence w0,…,wP−1w_0, \ldots, w_{P-1} of distinct nn-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 h0=0,h1,…,hn−1h_0 = 0, h_1, \ldots, h_{n-1} with (wt)j=(wt+hj)0(w_t)_j = (w_{t+h_j})_0 for all tt and jj (indices mod PP). Hiltgen, Paterson and Brandestini [6] introduced these codes for absolute rotary encoders. They showed that for n≥3n \geq 3 such a code contains neither 0n0^n nor 1n1^n and that 2n2n divides PP. For prime nn the period is therefore at most 2n−22^n - 2, and a code of period 2n−22^n - 2 is called optimal. For nn a power of 2, Etzion and Paterson [2] constructed codes of period 2n−2n2^n - 2n, and Schwartz and Etzion [12] showed that this is best possible.

For prime nn, optimal codes were known for n≤19n \leq 19. Mütze's survey [9, Section 3.12] records that existence “has been tested and confirmed for all prime n≤19n \leq 19” 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 2n−22^n - 2 as the optimal period with evenly spread heads for all primes n≤29n \leq 29, 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 n≥23n \geq 23.

Mütze [9, Section 3.12] describes the standard reduction. For prime nn every nonconstant necklace has nn distinct rotations, and an optimal code arises from a cyclic Gray code through the (2n−2)/n(2^n - 2)/n 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 Hamiltonianity of the necklace graph QnQ_{n}, whose vertices are the nonconstant necklaces of length n 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 n≤37n \leq 37, that consecutive necklaces in this order differ in a single bit except immediately after powers of odd primes (in their sense). For prime nn this says that their list is a Gray path through all necklaces, from 0n0^n to 1n1^n; 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 ⟨0n−11⟩\langle 0^{n-1}1 \rangle to ⟨01n−1⟩\langle 01^{n-1} \rangle, which are not adjacent for n≥5n \geq 5. We close it explicitly.

Theorem A. Let n≥7n \geq 7 be prime. If the Degni–Drisko list of necklaces of length n is a Gray path, then QnQ_n has a Hamilton cycle, obtained from the list by (n−3)/2(n-3)/2 Pósa rotations at explicit pivots, and an optimal single-track Gray code of length n and period 2n−22^n - 2 exists.

Theorem B. Optimal single-track Gray codes exist for every odd prime n≤47n \leq 47. For n≤37n \leq 37 the codes are given explicitly and each has been checked word by word by a verifier independent of the generator (for n=3n = 3, Lean's kernel); for n=41n = 41, 43 and 47 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 n≠2n \neq 2 the Degni–Drisko property at nn, an explicit hypothesis, implies the existence of a single-track Gray code of length nn and period 2n−22^n - 2; its proof uses only Lean’s standard axioms. The hypothesis there, adjacency in QnQ_n of consecutive necklaces of DnD_n, 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 n=5n = 5, by computation. For 7≤n≤377 \leq n \leq 37 it was verified by Degni and Drisko [1], and for n=37n = 37, 41, 43 and 47 by the computations of Section 4.