Pyramid Schemes for Eating M&Ms: Enumeration, Generation, and Gray Codes
Abstract
Consider the following problem. You have a rainbow pyramid of M&Ms with rows. For example, when you may have one red, two orange, three yellow, and four green . You want to eat of the M&Ms in such a way that the remaining M&Ms can be rearranged into a rainbow pyramid with rows. Two approaches are distinct if a different number from a particular row are eaten. In other words, we only care about the multiset of row frequencies (or colours) that are eaten and not the order in which they are eaten. One solution eats one M&M per row (e.g., ). Another eats the entire bottom row (e.g., ). How many different solutions are there? We show that the answer is . Furthermore, each solution can be naturally encoded with combinatorial objects enumerated by including binary words of length , compositions of , and subsets of . Less obviously they are encoded by M&M permutations where each value in is at most one position to the right of its position in the identity (e.g., , , , for ).
What if at most from each row can be eaten? When the only solution is to eat one of each colour. Otherwise, the solutions are counted by Fibonacci (), Tribonacci (), Tetranacci (), and so on, up to (). Furthermore, solutions can be naturally encoded by limited versions of the aforementioned objects including binary strings avoiding the substring and M&M permutations where values are limited by moving at most positions to the left.
Motivated by the works of Samuel Beckett, we consider minimal-change orders of the solutions. We obtain a satisfying result by filtering the binary reflected Gray code to words avoiding . For example, when we have and the words avoiding are , , , , where is the limit on the run-lengths of s. Our bijection then creates solutions that differ in by a single M&M , , , , . Thus, Beckett’s character Murphy can imagine every experience by changing one M&M at a time.
The generalized Gray code was previously defined recursively [Bernini et. al Acta Informatica 2015] with its change sequence supporting amortized -time generation [Arndt Matters Computational 2010]. We uncover a simple greedy definition – flip the leftmost bit that creates a new binary word avoiding starting from – and a successor rule that supports loopless worst-case -time generation. Furthermore, the corresponding limited M&M permutations are greedily generated by swapping the smallest value (or the leftmost pair of adjacent values) that gives a valid new permutation (e.g., , , , , for and ).
We also consider a relaxed version of the problem in which the initial pyramid’s rows have respective widths . Here the answer is an -term product that we refer to as a flatorial number. Furthermore, the solutions are represented by a generalization of M&M permutations in which each symbol can appear at most positions to the right of its position in the identity. We complete our investigation by showing that eight distinct classes of permutations are enumerated by flatorial numbers.
Keywords and phrases:
combinatorial enumeration, generation, Gray code, loopless algorithmFunding:
Brett Stevens: supported by the Natural Sciences and Engineering Research Council of Canada (funding reference number RGPIN-2023-04668).Copyright and License:
2012 ACM Subject Classification:
Mathematics of computing Combinatorial algorithms ; Mathematics of computing Combinatorics on wordsEditor:
John IaconoSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
We investigate a fun problem invented by the second author while eating colourful pyramids of chocolate M&Ms. In brief, an M&M pyramid of height has a top row of one M&M, a second row of two M&Ms, a third row of three M&Ms, and so on, with the property that the colours are the same on each row and different on distinct rows. The M&M Problem asks for the number of distinct ways that of the M&Ms can be removed (i.e., eaten!) so that the remaining M&Ms can be rearranged to form an M&M pyramid of height . Here we care only about the quantity of each colour eaten and not the order that they were enjoyed111In other words, we don’t care if you “eat the red ones last.” This was a slogan for the Canadian version of this type of chocolate candy known as Smarties. In the United States you can also play this game with Smarties although they are the pastel-colored discs that are known as Rockets in Canada! [3, 14].. Figure 1 illustrates that the answer for is . In particular, note that leftmost solution eats the entire bottom green row while the rightmost eats one of each colour.
Section 2 answers this snack-sized problem by proving that there are solutions. Furthermore, each solution is naturally represented as a binary string of length , a composition of , a subset of , or as a permutation of in which each symbol appears at most one position to the right relative to its natural position in the identity .
In Section 3 we lower the number of possible solutions using a parameter . In the limited version of the problem at most of each colour can be eaten. There is one solution when (i.e., eat one of each colour). Otherwise, the answers are Fibonacci (), Tribonacci (), Tetranacci (), and so on (with the base cases omitted), up to the unlimited problem with solutions (). The corresponding objects are also limited in natural ways, including binary strings avoiding and permutations in which each value is at most one position to the right and positions to the left relative to the identity.
Our first result implies that the number of M&M permutations of is . Perhaps the most well-known class of permutations of this size is peakless permutations. Those permutations play a central role in the recent Combinatorial Generation by Permutation Languages series [25] which is currently on its seventh entry [9, 10]. In particular, the bijection between peakless permutations and binary strings laid the foundation for generalizations of the binary reflected Gray code to larger permutation languages. In this paper we provide a swap Gray code for M&M permutations meaning that successive permutations differ by swapping consecutive entries. The order is visualized as a weaving pattern in Figure 3(a) (using the ribbon style from [40]). If the pattern looks familiar, it is because it is identical to the binary reflected Gray code! Indeed, the crossings in 3(a) line up exactly with the twists in Figure 3(b). Pleasantly, we obtain similar Gray codes for the corresponding objects with parameter (or ) including binary strings avoiding , and M&M permutations in which each value moves at most positions to the left. Furthermore, the corresponding rearranged pyramids (see Figure 6) differ in the colour of a single M&M!
In Section 5 we turn our attention to generation algorithms. We explain how the Gray codes parameterized by (or ) are generated by simple greedy algorithms. For example, the bitstrings avoiding are generated by greedily flipping the leftmost bit, while greedily swapping the leftmost pair of values produces the M&M permutations where each value moves at most positions to the left. Surprisingly, the two greedy algorithms use the same change sequence with bit flipping when adjacent values swap. The same order also produces a pleasant Gray code for the rearranged M&M pyramids: the colour of a single eaten M&M is changed.
While the algorithms in Section 5 are simple, they are not efficient due to memory usage. In Section 6 we derive the first successor rule for the Gray code of binary strings avoiding . This allows us to create generation algorithms that are memoryless (i.e., previously generated objects are not remembered) and loopless (i.e., worst-case -time per object) which is optimal in terms of time efficiency.
In Section 7 we return to counting M&Ms, but this time we raise the number of possible solutions by introducing a parameter . In the relaxed problem we widen (or narrow) the pyramid with rows as follows. The top row has M&Ms, the second row has M&Ms, the third row has M&Ms, and so on, except that each row also has width at most . The initial shapes for are shown in Figure 4. The goal remains the same: eat M&Ms so that the remaining M&Ms can be rearranged into a standard pyramid shape with rows.
-
When there is only one solution as the initial shape is a pyramid with rows.
-
When we have the original problem so the number of solutions is .
-
When or the number of solutions is since any permutation of the colours is possible for the remaining pyramid.
More generally, we prove that the number of solutions is the -term product which we refer to as a flatorial number.
2 The M&M Problem
In North America many holidays involve the giving and receiving of treats that often come in different colours, but are otherwise similar. At Easter there are Cadbury’s mini eggs; at Halloween there are many examples including M&Ms, Resses Pieces, Rockets, Smarties, Skittles; at Christmas there are Hershey’s kisses in holiday colours; Chocolate Chanukkah gelt is sometimes given during Hanukkah which varies in size (or coin denomination) rather than colour. Different candies come in different numbers of colours as shown in Table 1 which can aid you in determining what confectionary to purchase as you explore the mathematics we discuss222Remember to brush and floss every day while engaging in such research.. To make our work as accessible as possible to a more healthy dietary exploration, we note that mixed nuts could easily include up to nine different types: almond, brazil, cashew, hazelnuts, macadamia, peanut, pecans, pistachios, walnuts. The problem originated with M&Ms and since Mars Wrigley Confectionery does produce at least nineteen different colours, allowing for large pyramids, we will discuss the problem in the context of M&Ms.
| Candy product | number of types (colours) |
|---|---|
|
Smarties |
8 |
|
M&Ms, Rockets |
6 |
| Skittles, Haribo gummy bears, Wine gums (shapes) | 5 |
| Cadbury’s mini eggs | 4 |
| Reese’s Pieces, Holiday Hershey’s Kisses | 3 |
When reaching into a bag of M&Ms and pulling out a handful, usually the distribution of colours is not uniform and some colours appear more often and some only a few times. Noticing this in a sample handful of M&Ms one time, the second author started to arrange them on the table in order of the number of times a colour appeared. Frequently these distributions were close to being a pyramid. While it is a natural impulse to start nibbling on some M&Ms from the handful, for a mathematician it was perhaps inevitable to start with the few necessary to create a true pyramid. Once having a pyramid with different rows (colours), the author wondered how he could eat of the M&Ms and obtain a pyramid with different rows.
Given a rainbow pyramid of height , in how many ways can we remove a total of so that the remainder can form a rainbow pyramid of height ?
The largest colour class in a pyramid with rows is size so it is clear that we must eat at least one M&M from the largest class. Once this is eaten there are now two colour classes that have size . One and only one of them must remain at the end when we produce a pyramid with rows, so we have a choice between eating one M&M from exactly one of these two colour classes. At each stage, after we have eaten one M&M from our choice of exactly two colour classes of size , all the colour classes of size and larger in the pyramid with rows will have been determined and no more M&Ms can be eaten from any of these size classes. We have two colour classes of size and the colour class of size in the pyramid with rows must be one of these. Therefore, we must eat one M&M from exactly one of these classes of size and an induction proves the following theorem.
Theorem 1.
There are ways to remove objects from a rainbow pyramid of height so that the remainder can form a rainbow pyramid of height .
Before moving on to other mathematical connections, we note a recent amusing experience. The second author was eating a rainbow pyramid of a more healthy food (cherry tomatoes) where he has a prejudice against the red tomatoes. Whatever rainbow pyramid he initially started with, he found once he reached the next smaller rainbow pyramid he was inevitably left without any red tomatoes due to this bias. Thus when there are red tomatoes in a rainbow pyramid with rows, instead of possible eating paths to the next rainbow pyramid, he was left with eating paths333Exercise left to the reader.. In this case, the red tomatoes were more numerous than the other colours and in his lunch the red colour class was often the largest and frequently he was left with no choices at all. Samuel Beckett has a delightful expression of how our prejudices, both positive and negative, reduce the fullness of our experiences in life (also featuring permutations) in his novel Murphy [4].
Murphy receded a little way into the north [of the park] and prepared to finish his lunch. He took the biscuits carefully out of the packet and laid them face upward on the grass, in order as he felt of edibility. They were the same as always, a Ginger, an Osborne, a Digestive, a Petit Beurre and one anonymous. He always ate the first-named last, because he liked it the best, and the anonymous first, because he thought it very likely the least palatable. The order in which he ate the remaining three was indifferent to him and varied irregularly from day to day. On his knees now before the five it struck him for the first time that these prepossessions reduced to a paltry six the number of ways in which he could make this meal. But this was to violate the very essence of assortment, this was red permanganate on the Rima of variety. Even if he conquered his prejudice against the anonymous, still there would be only twenty-four ways in which the biscuits could be eaten. But were he to take the final step and overcome his infatuation with the ginger, then the assortment would spring to life before him, dancing the radiant measure of its total permutability, edible in a hundred and twenty ways!
Overcome by these perspectives Murphy fell forward on his face in the grass, beside those biscuits of which it could be said as truly as of the stars, that one differed from another, but of which he could not partake in their fullness until he had learnt not to prefer any one to any other.
Murphy is “wrestling with the demon of gingerbread” when he is distracted by a woman speaking. While in conversation the woman’s dog eats all the biscuits except the ginger.
There are other combinatorial classes of objects that have cardinality . Here we consider binary strings, subsets, compositions, and a special type of permutation, and show that they have natural bijections to our rainbow pyramid solutions. These results are then summarized in Theorem 2 and illustrated in Example 3 and Figure 5.
In the sequence of binary choices outlined above, we start by eating one M&M from the largest class. Then at stage we are always choosing between eating one of the existing colour class of size or one of the colour class most recently eaten. If we encode these choices as respectively, then we form a bijection to the binary strings of length . We note that the sequence of decisions are made in time as the are listed from right to left in this string. If , then we will have the opportunity to eat more of that colour, but if then we will not. Thus if this indicates that no M&M of colour is ever eaten and that colour will be the colour class of size in the obtained rainbow pyramid. Thus the values of for which are precisely the colours where at least one M&M of that colour are eaten. The positions of the zeros give a bijection to the subsets of via the “uneaten map” and the positions of the ones give another bijection to the subsets of via the “(at least one) eaten map” (disregarding the colour which must always be eaten).
Labelling the M&Ms in the colour class of size with the label for simplicity, we can form the sequence of the number of s that we ate, left to right, from 1 to , “eaten representation”. In eaten representation we will say that is the number of items labelled that are eaten. This is an ordered summation to , allowing zeros, such that the summand in position must be no larger than and the summand in position is at least 1. Observe that the position of the zeros match the positions of the zeros in the binary string representation. If we drop the zeros we obtain a simple composition of and thus a map from the eaten representation to the compositions of . We claim that this map is injective. It is easy to check that this is true for small values of . For an induction, now suppose that there are two eaten representations, and , mapping to the same composition of and that we have the smallest value of for which this is possible. Since at least one item labelled is eaten we know that the rightmost summand of the composition must be the number of s eaten, and that these two values agree in both composition and thus agree in the eaten representation. For each eaten after the first, the next smaller colour class is ignored. Thus and for both eaten representations the next rightmost non-zero value is in position . Thus the eaten representation from position left is a valid eaten representation for a rainbow pyramid of height and inducton gives the result. Since the number of eaten representations and number of compositions of both have cardinality this gives a bijection between the two sets.
Again labeling the items in the colour class of size with label , the starting rainbow pyramid is naturally represented by the identify permutation . After having eaten the M&Ms to obtain the new rainbow pyramid, add back one M&M of each colour to produce another pyramid of the original size and consider its corresponding permutation, , of . The positions of the zeros in the binary string representation (and also the zeros in the eaten representation) correspond exactly to the set of colours that were not eaten at all. These colour classes stayed the same size and then add the one additional M&M added so in have moved right exactly one position from their starting position. Since there were items labeled in the starting rainbow pyramid, no label can move more than one position to the right. The new permutation is uniquely determined by what has been eaten so this gives a bijection to the set of permutations of size such that each symbol appears at most one position to the right relative to its natural position in the identity . That this family had cardinality was observed by Arndt [2] in work that we discuss later on.
Theorem 2.
The following objects are equinumerous and each pair has -time bijections.
-
1.
Solutions to the rainbow pyramid problem
-
2.
Binary strings of length .
-
3.
Subsets of .
-
4.
Compositions of .
-
5.
Permutations of where each value appears at most one position to the right relative to its natural position in the identity permutation. These are the M&M permutations .
Example 3.
Suppose that we start with a rainbow pyramid of height 6 which is naturally represented by the permutation and illustrated at the left in Figure 5(a). Consider the solution that eats M&Ms in the following order: . Eating the first is forced and that fact that the next two M&Ms eaten are s indicates that : we did not eat a or when we had the opportunity. Our choice to then eat a instead of another gives . Our remaining choices are encoded with and giving binary representation . The eaten representation simply records how many of each colour we ate: . This corresponds to the composition of .
At the end our rainbow pyramid is represented by . When we add back one M&M of each colour we have the permutation representation . In this permutation , and have moved to the right one position relative to their position in the identity permutation. Note that these colours which moved to the right are precisely the colours which were not eaten and correspond exactly to the positions of zeros in the binary string representation. Furthermore subtracting one from the non zero values of the eaten representation give the number of places to the left these colours moved: moved positions to the left; moved position to the left and moved positions to the left.
3 A Limited M&M Problem with Parameter
Suppose that we want to avoid eating too many M&Ms of the same colour. More specifically, we limit ourselves to eating at most copies of any given colour, where is some fixed constant. This leads to a generalization of our original problem parameterized by .
Given a rainbow pyramid of height , in how many ways can we remove at most of each colour so that the remainder can form a rainbow pyramid of height ?
Once again we consider solutions as unordered selections of colours, and every solution removes exactly of the M&Ms. Now let us consider how our various representations are modified under this chromatic limitation. For some objects it is convenient to use .
- Binary strings of length .
-
As in the original problem we know that at least one copy of colour is removed, and bit when at least one copy of colour is removed. However, not every binary string of length encodes a valid solution when the use of each colour is limited. Indeed, the following points show that a solution is valid, if and only if, the binary string avoids substrings of the form (or equivalently, is the longest run of s allowed).
-
Substring indicates copies of colour are removed.
-
Suffix indicates copies of colour are removed.
-
- Subsets of .
-
Consecutive values in the subset (union with ) differ by at most .
- Compositions of .
-
Each part is at most .
- Permutations of .
-
Each symbol can move at most one position to the right and at most positions to the left relative to the identity.
These observations lead to the following theorem. Note that the correspondences with the M&M Problem are new, while those involving the other M&M objects can be found in various textbooks on combinatorics [28].
Theorem 4.
The following objects are equinumerous.
-
1.
Solutions to the rainbow pyramid problem with limit .
-
2.
Binary strings of length without any substring of the form .
-
3.
Subsets of union with in which consecutive values differ by at most .
-
4.
Compositions of in which each part is at most .
-
5.
Permutations of in which each symbol appears at most one position to the right and at most positions to the left relative to the identity.
4 Gray Codes
Given a combinatorial object of a particular type and size (e.g., -bit binary strings, permutations of , binary trees with nodes) a Gray code orders the objects such that consecutive objects are similar to each other in some particular way. Humans with various backgrounds have been constructing these types of patterns for hundreds of years. For example, bell-ringers in the 1600s created plain changes [16, 52] which orders the permutations of so that consecutive permutations different by swapping two consecutive values in one-line notation (e.g., , , , , , for ). Plain changes was rediscovered multiple times in the 1960s leading to its alternate moniker: the Steinhaus-Johnson-Trotter algorithm [53, 30, 56]. Another Gray code for permutations, this time using prefix-reversals, was discovered in the late 1700s by Klügel [29] (e.g., , , , , , ).
The obsession with listing all possibilities with small changes between them also extends to Beckett and his works. In particular, Quad [5] is a play with four actors in which every non-empty subset of actors appear onstage in successive scenes with a single performer entering or exiting to create the next scene. Furthermore, Beckett required that the actor who had been offstage the longest would always be the one who entered. This idea led to the notion of a Beckett Gray code [49, 60, 15]. Similarly, it is easy to imagine Murphy obsessing over similar issues when eating his cookies.
In this section we discuss the binary reflected Gray code, the concepts of sublist and simultaneous Gray code, and how these ideas apply to our M&M objects parameterized by . The results are illustrated in Figure 6.
4.1 Binary Reflected Gray Code
The most widely known Gray code is the binary reflected Gray code (BRGC). The BRGC orders the binary strings with bits so that consecutive strings differ in a single bit (e.g., , , , , , , , for ). The order is attributed to Frank Gray at Bell Labs due to his 1953 patent involving televisions [21]; see Ruskey [41] and Knuth [32] for additional historical details. In particular, the attribution is a strong case of Stigler’s Law of Eponymy [55] as the order appeared in an earlier patent also from Bell Labs [54]!
The word reflected refers to writing a list of strings in reverse (e.g., Algorithms with Fun) and the reflected code (as Gray called it) is constructed recursively by prefixing to every string of length , followed by prefixing to every string of length in reflected order. This recursive construction is formalized below with base case (i.e., the empty string)
| (1) |
where concatenates a bit to every string in a list, and the commas combine two lists.
Example 5.
.
A change sequence is a list of successive changes made in a Gray code. For the binary reflected Gray code, the list of indices that are flipped form the well-known binary ruler sequence (A001511) [38]. The sequence can be defined recursively as follows with base case (i.e., empty sequence)
| (2) |
It would be reasonable to expect the second copy of to be reversed due to the reflection in . This is correct, but not necessary, as is always a palindrome.
Example 6.
The bit indices that are flipped in are , so the indices that are flipped in are .
4.2 Sublist Gray Codes
Given a Gray code order for a set of objects, we can create a list of any subset by considering the corresponding sublist. In other words, we can use the induced suborder. If the result is also a Gray code, then we refer to it as a sublist Gray code. One of the first results of this type is that inducing the binary reflected Gray code on fixed-weight binary strings (i.e., strings with a fixed number of s) produces a transposition Gray code [58].
The binary reflected Gray code also provides a sublist Gray code for the strings that avoid in which successive strings differ by the flip of a single bit. This result was proven as part of a larger study on avoiding specific substrings (also known as factors) in the reflected Gray code for -ary strings [6]. More generally, Gray codes for words avoiding certain factors have been studied for at least 30 years [50]. We denote the set of binary strings avoiding as , where provides the limit on the longest run of s that is allowed, and the corresponding sublist of the binary reflected Gray code by .
Example 7.
The sublist of with limit is a flip Gray code .
Alternatively, can be defined directly by recursion with base case
| if | (3a) | ||||
| if . | (3b) |
Note that the strings are ordered by their rightmost with the special case when .
Example 8.
and the sublists produce the order .
These orders were generated in amortized -time by Arndt [1]. He recursively generated the change sequence , which we refer to as the ruler sequence with limit , as follows
| if | (4a) | ||||
| if . | (4b) |
with base case . These sequences are not always palindromes, so the reflections are relevant. Note that the in (4a) gives the special transition in (3a).
Example 9.
is the change sequence of in Example 8. The first two sublists are original ruler sequences (which are palindromes) so the sequence is .
4.3 Simultaneous Gray Codes
The previous section explained that one of our M&M objects has a Gray code.
Theorem 10 ([1, 6]).
There is a flip Gray code for the binary strings that avoid . In fact, the limited binary reflected Gray code is a sublist Gray code of the binary reflected Gray code and its change sequence is the limited ruler sequence .
What does Theorem 10 tell us about Gray codes for other M&M objects? Given two sets of objects and and a bijection between them , a simultaneous Gray code is an order that is a Gray code for both and . In other words, if you take the Gray code for and apply to each object, then a Gray code for is obtained.
Simultaneous Gray codes often exist when the bijection between objects is very simple. For example, the binary reflected Gray code is a simultaneous Gray code for binary strings, compositions, and subsets [37] and these results generalize nicely based on the limit . On the other hand, simultaneous Gray codes are more difficult to attain when the bijections are non-trivial. For example, there are hundreds of objects counted by the Catalan objects [51], yet simultaneous Gray codes for them were quite rare [43, 17] prior to recent developments [25, 22] (see [39] for further discussion of this point).
This brings us to M&M permutations. It turns out that the binary reflected Gray code provides a swap Gray code for . Since the swap operation is used in plain changes, and since “plain” M&Ms are also known as “regular” M&Ms, we’ll refer to this Gray code as regular order for M&Ms and denote it . Arndt observed that swap Gray codes are obtained in the same way for any limit as stated in the following theorem.
Theorem 11 ([1]).
There is a swap Gray code for the M&M permutations with limit . In fact, the limited regular order is simultaneous with the limited binary reflected Gray code . Furthermore, the change sequence is both the sequence of swapped smaller indices or values.
Example 12.
has change sequence . Therefore, we can swap the corresponding indices to obtain the limited regular order as ; alternatively, the sequence specifies the smaller value that swaps over a larger value .
A simple consequence of Theorem 11 is that our rearranged M&M pyramids also have a simple and delicious Gray code. Each swap in an M&M permutation corresponds to swapping two adjacent rows in the corresponding rearranged pyramid, or replacing the top row (consisting of one M&M) with the missing colour. Note that these changes can be made by changing the colour of a single M&M. In other words, successive solutions to an M&M Problem (with limit ) differ by the colour of a single eaten M&M!
5 Greedy Algorithms
In the half-century following Gray’s patent, recursion was used in various ways to create new Gray codes, as reflected in Savage’s survey from the late-1990s [45]. In the last decade, it has become more common to view the binary reflected Gray code as the result of a simple greedy algorithm. The algorithm starts the order with the string , then it repeatedly creates a new next string by flipping the leftmost bit in the most recent string. Or less verbosely: flip the leftmost bit. This process continues until no flips are possible (i.e., flipping any bit in the most recent string recreates a previous string).
Example 13.
The binary reflected Gray code begins . Now consider the next step using the greedy algorithm. It first tries to flip the leftmost bit of the most recent string , however, this fails as is already in the list. Similarly, is already in the list. But its third priority succeeds as is not in the list. So is added to the end of the list, and the algorithm continues by operating on it.
Many previous Gray code constructions can be re-expressed in terms of greedy algorithms. For example, plain changes and Klügel’s order from Section 4 are generated from by swapping the smallest value and reversing the shortest prefix, respectively [59].
The greedy perspective has led to a surge of new results, including fun results on pancake flipping [19] and matroids [34]. It has also been used to generate various subsets of binary strings [57, 27] and solve multiple conjectures [33, 33]. However, the biggest success of the approach is the Combinatorial Generation via Permutation Languages series of papers444The smallest “zig-zag language” is another set of permutations enumerated by . The peakless permutations avoid the patterns and [7] and the connection between them and the binary reflected Gray code motivated us to consider the Gray codes in this paper.. The series was initially announced in an extended abstract and poster at Permutations Patterns 2019 [23] before appearing as conventional conference and journal papers [24, 25]. Since then it has grown to its seventh entry [9, 10] with a generalization involving multiset permutations (or -words) recently announced [12]. The greedy approach is featured in Mütze’s updated survey of Gray codes [36] which is regularly updated on arXiv [35] and also appears as a dynamic survey in the Electronic Journal of Combinatorics [37].
5.1 New Results
Pleasantly, the Gray codes discussed in Section 4 also have simple greedy algorithms. In fact, the most difficult part of the algorithms is describing the first object. We know from their sublist constructions that the Gray codes should start with the first string in the order induced by the binary reflected Gray code, but what are those strings?
Before providing formulae for these strings, it is helpful to first consider the mere existence of swap Gray codes for M&M permutations . Consider the permutation . Note that every value is already one position to the right, except for . Therefore, only one swap is valid, namely the swap of the first two positions. Similarly, there is only one valid swap for . Therefore, any swap Gray code for must start and end at these two permutations. This illustrates how vitally important the choice of the initial object is in this context.
Given a Gray code we let and denote the first and last objects in the order, respectively. (The values of are not needed to describe our greedy algorithms, but they are helpful in understanding why they work.)
Now consider the first string in the limited binary reflected Gray code . The order begins with strings with suffix , so has suffix so long as . Similarly, the suborder of strings with suffix also starts with suffix , so has suffix if . Continuing this argument we see that has suffix but not suffix as that would be invalid. In other words, it has suffix . The value indicates a list reflection, so the corresponding suborders begins with suffix and ends with suffix . Thus, has suffix . The second copy of indicates a second list reflection, so we return to our previous arguments. This results in having the form where the start of the string depends on and . The last string in the order can be established with a similar argument.
Lemma 14.
The first string in the limited binary reflected Gray code is the unique length string of the form .
Lemma 15.
The last string in the limited binary reflected Gray code is the unique length string of the form with the caveat that its first bit must be .
Example 16.
The first and last strings in are based on the repeating pattern . Hence, and .
The first and last strings in the limited regular order are simply the M&M permutations that correspond to the above binary strings.
Theorem 17.
The limited binary reflected Gray code of binary strings avoiding is generated by repeatedly flipping the leftmost bit that creates a new string starting from the string in Lemma 14. Similarly, the limited regular order of M&M permutations is generated by swapping the leftmost pair of adjacent values (or the smallest value) starting from the corresponding permutation.
Note that the instruction to “swap the smallest value” could be ambiguous. For example, given we wouldn’t know whether to first consider or . However, this distinction never arises as at most one of the swaps is valid and creates a new permutation. This phenomenon is common in greedy Gray code algorithms. In particular, it is present in the greedy description of plain changes and many of its generalizations [24].
Example 18.
We know , so . Indeed begins . Now consider the next step of the greedy algorithm. It first tries to swap the leftmost pair of indices, but is already in the list. Then it tries to swap the next pair of indices but this is not a valid swap as is more than one position to the right of its position in the identity. Similarly, is invalid as is too far to the right. Finally, is successful. Alternatively, the next step swaps the smallest possible value with failures for value ( and ) then value ( and ) then value () before success in exactly one direction for value ().
6 Efficient Algorithms
While the algorithms in Section 5 are simple, they are also inefficient. In particular, remembering previously generated objects uses exponential space relative to even when (as the Fibonacci sequence grows like for the golden ratio ).
In this section we develop a successor rule for the binary strings avoiding . In other words, we show how to directly map any such binary string into the next binary string in the Gray code without any additional information. By carefully maintaining additional space, we are able to repeatedly apply the successor rule in constant time. In other words, we create loopless algorithm which generates successive objects in worst-case -time. Loopless algorithms were pioneered by Ehrlich [18] and are best possible in terms of time-complexity.
To better contextualize these results we first differentiate between enumeration algorithms and generation algorithms.
6.1 Generation Algorithms vs Enumeration Algorithms
The goal of an enumeration algorithm is to create every object. They are especially useful for complicated objects when the goal is to store them in a file for subsequent analysis (e.g., see [11] and [13]). Alternatively, we may want to count (i.e., enumerate!) the objects. In these situations it makes sense to measure the time-complexity in terms of the total time or average time (i.e., total time divided by the number of objects). Regardless of the application, these algorithms are often run from the command-line with output piped to a file (e.g., ./enumerate n > n.txt.)
In other situations we want to start iterating over the objects immediately, and we won’t necessarily complete the process. For example, if we are searching for an object that solves a problem, then we may wish to stop once one is found. In these situations it makes sense to measure how much time is taken to generate the first objects for all possible values of . This leads to the notion of a generation algorithm and amortized time or worst-case analyses. Regardless of the exact application, these algorithms are often used as iterators within modern programming languages that support them (e.g., for x in generate(n)).
When an application doesn’t store or print the objects it can be possible to process each one in -time (i.e., less than -time). In the shared object model the generation algorithm and application share one object; the generation algorithm modifies this object and notifies the application that the next possibility is ready. To fully take advantage of this model, the generation algorithm must make small changes either in an amortized or worst-case sense. It must also avoid most common types of recursion and it should communicate each change so that the application can avoid scanning the shared object. For example, imagine solving a Knapsack Problem by generating the binary reflected Gray code; each bitstring is an incidence vector of items and each bit-flip changes the associated value of the selected items by adding or subtracting one value. In fact, it is possible to generate and evaluate each feasible solution in amortized -time [46].
Note that the nomenclature advocated here by the authors is new and non-standard. For example, Ruskey’s unpublished but widely used and often cited Combinatorial Generation [41] textbook primarily focuses on enumeration algorithms555Its preface offers a more accurate (but less catchy) title: Exhaustive Listing of Combinatorial Objects!.
6.2 Successor Rule
The successor rule for binary strings in without any limitation is well-known [31]. It flips the first bit if the weight of the string (i.e., its number of s) is even, and otherwise it flips the bit to the right of the leftmost . This is presented below.
Definition 19.
Let with weight . Its successor is obtained by flipping the bit at index which defined as
| if is even | (5a) | |||
| otherwise | (5b) | |||
where is the smallest index with .
Note that and are undefined precisely when . This is expected and desired behaviour as is the last string in , so it has no successor.
Example 20.
Now we derive a successor rule for the binary strings avoiding in order. While the limited Gray code has been discussed elsewhere [1, 6] to our knowledge the successor rule has not previously been determined.
The run-length encoding of binary string is
| (6) |
where each and are positive except possibly for (i.e., the first run of s) and (i.e., the last run of s). This partitions the binary string into blocks which are maximal substrings of the form (i.e., some number of s followed by some number of s). We let be the starting index of the th block.
Example 21.
Let . Its blocks are , , and .
-
The run-lengths of the blocks are , , and .
-
The starting indices of the blocks are , , and .
Now we express the successor rule where provides the index to flip. To make sense of the definition, note that the successor either flips the first bit (i.e., ), the second bit in a block (i.e., ), or the last bit (i.e., ). Also note that if a block equals (i.e., and ), then we cannot flip its second bit as it would create . The final consideration is that we don’t want to flip the bit that creates the predecessor (i.e., the previous string) and this leads to condition (b) in the definition of .
Definition 22.
Let with run-length encoding and weight . Its successor flips index defined as
| if is even and | (7a) | |||
| otherwise, if exists | (7b) | |||
where is the smallest index of a block satisfying (a) and (b) if is even or . If neither case holds, then and are undefined.
6.3 Loopless Implementations
To create a loopless algorithm for we need to be able to apply the successor rule in -time in the worst-case. In other words, we need to store additional information that allows us to determine in -time, and we need to be able to update any such information in -time.
Our additional information consists of the weight of the current string and two sequences.
-
The first sequence keeps track of where the runs of s begin in the current string.
-
The second sequence allows us to skip over each block of the form once. These skips are needed to avoid creating illegal substrings via the successor rule and several such blocks may be simultaneously skipped over at the same time.
Given this additional information we can apply the successor rule in -time. Similarly, we can update the additional information in constant-time. More specifically, we use the locations of consecutive runs of s and the values of surrounding bits to determine when a block of the form has been created. We also store the skipping information as offsets that allows us to clear the need to skip an arbitrary number of consecutive blocks in constant-time. This leads to the following theorem.
Theorem 24.
The limited binary reflected Gray code can be generated by a loopless algorithm.
The loopless algorithm for generates each successive entry in the change sequence (i.e., the index of the bit flipped) in worst-case -time. Therefore, we can use the same algorithm to generate the limited regular order.
Theorem 25.
The limited binary regular order can be generated by a loopless algorithm.
The authors have implemented these algorithms in Python and the programs are available upon request.
7 A Relaxed M&M Problem with Parameter
Now we consider the relaxed problem initially seen in Figure 4(b). When there is only one solution (i.e., no M&Ms are eaten), the original problem is obtained with , and problems with involve eating more M&Ms (which is arguably the real goal!).
7.1 Flatorial Numbers
Mathematicians define the factorial numbers as and may shudder when Computer Scientists write loops that instead compute . A more productive distinction involves “flattening out” the latter product once it reaches a specific value.
Definition 26.
The flatorial number for and is
For example, the flatorial for and is . The flatorial triangle appears below for . Observe that the triangle’s diagonal is , and is its diagonal. It is also the case that the diagonal is , as .
7.2 Enumerating the Relaxed Solutions
Theorem 27.
The number of solutions to the M&M Problem with height and relaxation is .
Proof.
Let be the colours of successive rows in the initial pyramid and be the colours of successive rows in the new pyramid. Note that there are exactly choices for the colour at the bottom of the new pyramid . More specifically, . This is due to the fact that only the bottom rows of the initial pyramid have width at least .
Suppose that the value of has been chosen, and now consider the choices for . Note that . This is because the bottom rows of the initial pyramid have width at least and the color has already been used. Also note that is not a valid colour when or . So the number of choices for is if or if .
In the same way, the number of choices for is if , or if , or if . As a result, the total number of ways of choosing is exactly .
Interestingly, the proof of Theorem 27 gives us another way to prove our original theorem. In the standard M&M Problem there are two choices for the M&M colour that will become row at the bottom of the new pyramid. More specifically, the choices are colour or colour as these are the colours with frequency at least in the initial pyramid. After this choice is made there are two choices for the colour of row in the new pyramid. More specifically, the choices are colour together with colour or colour (depending on which is not used for the bottom row of the new pyramid). This continues until we reach the result of . To visualize this argument note that exactly half of the eight new pyramids in Figure 1(b) have a bottom row of green while the others have a bottom row of yellow, then for each of those four pyramids there are two different colours on the next row, and so on.
8 One-Way Bounded Permutations
Given the results of the previous sections, it becomes natural to ask if there are other classes of permutations counted by or more generally the flatorial numbers. In this section we use a tree-based argument to prove that eight distinct classes have this property [26]. These classes are one-way bounded permutations on for a fixed parameter .
Let be the set of all permutations of in one-line notation. Consider the following two subsets of for and a fixed parameter explained below.
| (8) | ||||
| (9) | ||||
The first set contains permutations in which implies . In other words, if a value appears further to the right than it does in the identity, then . Since in (8), only value (underlined) can appear to the right of its position in . The second set contains permutations whose inversion vector666The inversion vector counts the smaller inverted values. That is, . (shown below) has for all . In other words, every value is inverted with at most smaller values. Since in (9), each value is inverted with at most one smaller value.
The two sets have the same cardinality; we will prove that this is true for all and . Moreover, this is true for eight classes of one-way bounded permutations that are listed in Definition 29. None of our equivalences are obtained by reversing indices and/or inverting values . These trivial modifications known as the symmetries of the square extend our results to equinumerous types. For example, the “low rights” in (8) become “high lefts” (i.e., implies ) by reversing and inverting. Our results are summarized in Table 2. While each type is fairly elementary, we were unable to find many references in the literature. Table 3 shows that only three of the types are listed in corresponding Oeis entries. Short descents with arise using classic pattern avoidance [7]: ). Some non-classical avoidance results also appear in the Oeis sequences [8, 20].
| low descents | short descents | low rights | short rights | large inverts | small inverts | early lefts | short lefts |
| insert value | insert value | swap value | swap value | insert index | insert index | swap index | swap index |
| small labels | large labels | small labels | large labels | small labels | large labels | small labels | large labels |
| Formula () | Oeis | short rights | short ascent | short lefts | ||
|---|---|---|---|---|---|---|
| A000079 | Arndt (2009) | |||||
| A025192 | Lewis (2006) | |||||
| A084509 | Knuth (2022) | |||||
| A179364† | Hardin (2010) | |||||
| A179365† | Hardin (2010) |
8.1 Family Trees of Permutations & One-Way Bounded Permutations
We consider four family trees of permutations. Each of these trees generate two of the classes of one-way bounded permutations given in Definition 29; the classes of permutations that these trees generate is shown in Table 2. Each tree has root and appears at level . At level , the branches are labeled with values . The branch always inserts as the rightmost symbol, and identity permutations are on rightmost paths. The remaining children are obtained as follows, where is the branch label.
-
Insert value tree: insert to the left of value .
-
Swap value tree: insert as the rightmost symbol, then swap it with the value .
-
Insert index tree: insert to the left of the index .
-
Swap index tree: insert as the rightmost symbol, then swap it with index .
For each family tree we consider two subtrees parameterized by a given value of .
-
The large label subtree includes branches with labels to at level .
-
The small label subtree includes branches with labels and to at level .
So at level the subtrees include branches with label and (at most) the smallest or largest remaining labels. Thus, the number of nodes at each successive level increases from up to giving Remark 28. Family trees and subtrees appear in Figures 8–8.
Remark 28.
A family subtree with large (or small) labels has nodes at level .
Definition 29.
Let be a permutation, its inverse, and its inversion vector. Value is an invert if it is inverted with some smaller value (i.e., ). For example, has , , and inverts and . We define the following one-way bounded permutations for a fixed .
-
(a)
has low descents, denoted , if implies .
That is, the smaller value in a descent is at most . For example, in each smaller value in a descent has . -
(b)
has short descents, denoted , if .
That is, each descent has height at most . For example, in , each descent has height . -
(c)
has low rights, denoted , if implies .
That is, only small values can move to the right. For example, in , only values have moved to the right. -
(d)
has short rights, denoted , if , or equivalently, .
That is, values can move at most spaces to the right. In , values have moved at most spaces to the right. -
(e)
has large inverts, denoted , if implies .
That is, any invert is inverted with at least smaller values. In , any invert is inverted with at least smaller values. -
(f)
has small inverts, denoted , if for all .
That is, each value is inverted with at most smaller values. For example, in , each value is inverted with at most smaller values. -
(g)
has early lefts, denoted , if implies .
That is, if a value is moved to the left, it must be in the first positions of . In , values that moved left are in the first positions. -
(h)
has short lefts, denoted , if , or equivalently, .
That is, values can move at most spaces to the left. For example, in , values have moved at most spaces to the left.
Theorem 30.
For all and , each of the sets of one-way bounded permutations in Definition 29 are counted by flatorial numbers, .
Proof.
By Remark 28, for each set of one-way bounded permutations, we need only show that the permutations in the set are exactly the nodes in the associated subtree with large labels or small labels. We illustrate this for low rights using the swap value tree with small labels; the other proofs are similar. A right is a value with .
Consider the swap value tree, in which each child of at branch is obtained by appending , and then swapping with for . Each branch creates a right: is now in position . Suppose . The children of in are exactly those with branches (in which rights with values at most are created), and branch (in which no right is created). On the other hand, if is a right in permutation , then will be a right in all descendants of , since either remains in its same position for branches , or moves further to the right at any branch . Therefore, if , then none of its descendants can be.
| (a) low descents | (b) short descents | (c) low rights | (d) short rights | ||||||||||||
| Name | Formula () | Oeis | reference | |||
|---|---|---|---|---|---|---|
| Mononacci | A057427 | |||||
| Fibonacci | A000045 | |||||
| Tribonacci | A000073 | |||||
| Tetranacci | A000078 | |||||
| Pentanacci | A001591 | |||||
| Hexanacci | A001592 | |||||
| Heptanacci | A122189 | |||||
| -nacci | A000079 | Arndt (2009): short rights | ||||
| A025192 | Lewis (2006): short ascents | |||||
| A084509 | Knuth (2022): short ascents | |||||
| A179364† | Hardin (2010): short lefts | |||||
| A179365† | Hardin (2010): short lefts |
9 Final Remarks
9.1 Summary
Our investigation started by trying to count the number of solutions to a fun eating problem. By varying the problem in two ways we obtain a natural progression of counting functions from to Fibonacci to to as shown in Table 5. This progression includes variations of Fibonacci numbers (e.g., Tribonacci) and the newly named flatorial numbers.
We also considered how to efficiently order and generate the solutions to the original and limited problems, including new foundational results on the limited binary reflected Gray code . Finally, to ensure that the fun never ends, we articulated eight distinct classes of permutations that are also enumerated by flatorials!
9.2 Future Work
A natural enumeration question arises from simultaneously applying our two parameters and . A natural Gray code question is if the solutions to our relaxed problem can be appropriated represented and ordered. A natural algorithmic question is if the new sets of one-way permutations can be efficiently generated.
References
- [1] Jörg Arndt. Matters Computational: ideas, algorithms, source code. Springer Science & Business Media, 2010.
- [2] Jörg Arndt. Permutations with special properties, pages 277–290. Springer Berlin Heidelberg, Berlin, Heidelberg, 2011. doi:10.1007/978-3-642-14764-7_11.
- [3] Linda J Barth. New Jersey Originals: Technological Marvels, Odd Inventions, Trailblazing Characters & More. Arcadia Publishing, 2018.
- [4] Samuel Beckett. Murphy. Picador, 1973.
- [5] Samuel Beckett. The Collected Shorter Plays of Samuel Beckett: All That Fall, Act Without Words, Krapp’s Last Tape, Cascando, Eh Joe, Footfall, Rockaby and others. Grove/Atlantic, Inc., 2010.
- [6] Antonio Bernini, Stefano Bilotta, Renzo Pinzani, Ahmad Sabri, and Vincent Vajnovszki. Gray code orders for q-ary words avoiding a given factor. Acta Informatica, 52(7):573–592, 2015. doi:10.1007/S00236-015-0225-2.
- [7] David Bevan. Permutation patterns: basic definitions and notation. arXiv preprint arXiv:1506.06673, 2015.
- [8] David Bevan, Derek Levin, Peter Nugent, Jay Pantone, Lara Pudwell, Manda Riehl, and ML Tlachac. Pattern avoidance in forests of binary shrubs. Discrete Mathematics & Theoretical Computer Science, 18(Permutation Patterns), 2016.
- [9] Sofia Brenner, Jean Cardinal, Thomas McConville, Arturo Merino, and Torsten Mütze. Combinatorial generation via permutation languages. vii. supersolvable hyperplane arrangements. arXiv preprint arXiv:2507.14327, 2025.
- [10] Sofia Brenner, Jean Cardinal, Thomas McConville, Arturo Merino, and Torsten Mütze. Traversing regions of supersolvable hyperplane arrangements and their lattice quotients. In Proceedings of the 2026 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 4953–4968. SIAM, 2026.
- [11] Gunnar Brinkmann, Gilles Caporossi, and Pierre Hansen. A constructive enumeration of fusenes and benzenoids. Journal of Algorithms, 45(2):155–166, 2002. doi:10.1016/S0196-6774(02)00215-8.
- [12] Samuel Buick, Madeleine Goertz, Amos Lastmann, Kunal Pal, Helen Qian, Sam Tacheny, Aaron Williams, Leah Williams, and Yulin Zhai. Exhaustive generation of pattern-avoiding -words. In Proceedings of the 23rd International Conference on Permutation Patterns, pages 107–110, 2025.
- [13] James Chapman, Judith Foos, Elizabeth J Hartung, Andrew Nelson, and Aaron Williams. Pairwise disagreements of kekulé, clar, and fries numbers for benzenoids: A mathematical and computational investigation. MATCH Communications in Mathematical and in Computer Chemistry, 80(1):189–206, 2018.
- [14] Paul Chrystal. The History of Sweets. Pen and Sword, 2021.
- [15] Mark Cooke, Chris North, Megan Dewar, and Brett Stevens. A note on Beckett-Gray codes and the relationship of Gray codes to data structures. Journal of Combinatorial Mathematics and Combinatorial Computing, 126:195–200, 2025.
- [16] Richard Duckworth. Tintinnalogia, Or, the Art of Ringing: Wherein is Laid Down Plain and Easie Rules for Ringing All Sorts of Plain Changes. Project Gutenberg, 2010.
- [17] Stephane Durocher, Pak Ching Li, Debajyoti Mondal, Frank Ruskey, and Aaron Williams. Cool-lex order and -ary Catalan structures. Journal of Discrete Algorithms, 16:287–307, 2012. doi:10.1016/J.JDA.2012.04.015.
- [18] Gideon Ehrlich. Loopless algorithms for generating permutations, combinations, and other combinatorial configurations. Journal of the ACM (JACM), 20(3):500–513, 1973. doi:10.1145/321765.321781.
- [19] Harrah Essed and Wei Therese. The harassed waitress problem. In International Conference on Fun with Algorithms, pages 325–339. Springer, 2014. doi:10.1007/978-3-319-07890-8_28.
- [20] Alice LL Gao and Sergey Kitaev. On partially ordered patterns of length 4 and 5 in permutations. The Electronic Journal of Combinatorics, 26(3), 2019. doi:10.37236/8605.
- [21] Frank Gray. Pulse code communication. United States Patent Number 2632058, 1953.
- [22] Petr Gregor, Torsten Mütze, and Namrata. Combinatorial generation via permutation languages. vi. binary trees. European Journal of Combinatorics, 122:104020, 2024. doi:10.1016/J.EJC.2024.104020.
- [23] Elizabeth Hartung, Hung P Hoang, Torsten Mütze, and Aaron Williams. Exhaustive generation of pattern-avoiding permutations. Permutation Patterns 2019, pages 81–83, 2019.
- [24] Elizabeth Hartung, Hung P Hoang, Torsten Mütze, and Aaron Williams. Combinatorial generation via permutation languages. In Proceedings of the Thirty-First Annual ACM-SIAM Symposium on Discrete Algorithms, pages 1214–1225. SIAM, 2020.
- [25] Elizabeth Hartung, Hung P Hoang, Torsten Mutze, and Aaron Williams. Combinatorial generation via permutation languages. i. fundamentals. Transactions of the American Mathematical Society, 375:2255–2291, 2022.
- [26] Liz Hartung, Brett Stevens, and Aaron Williams. One-way bounded permutations and the flatorial numbers. In Proceedings of the 23rd International Conference on Permutation Patterns, pages 55–58, 2025.
- [27] Nathanaël Hassler, Vincent Vajnovszki, and Dennis Wong. Efficient generation of some greedy binary Gray codes. RAIRO-Theoretical Informatics and Applications, 59:16, 2025. doi:10.1051/ITA/2025014.
- [28] Silvia Heubach and Toufik Mansour. Combinatorics of compositions and words. Chapman and Hall/CRC, 2009.
- [29] Carl Friedrich Hindenburg. Collection of combinatorial-analytical treatises, volume 2. ben Gerhard Fleischer the Younger, 1796.
- [30] Selmer M Johnson. Generation of permutations by adjacent transposition. Mathematics of computation, 17(83):282–285, 1963.
- [31] Donald E Knuth. Art of Computer Programming, Volume 4, Fascicle 2, The: Generating All Tuples and Permutations. Addison-Wesley Professional, 2013.
- [32] Donald E Knuth. Art of Computer Programming, Volume 4, Fascicle 4, The: Generating All Trees–History of Combinatorial Generation. Addison-Wesley Professional, 2013.
- [33] Bowie Liu, Dennis Wong, Chan-Tong Lam, and Sio-Kei Im. Generating pivot Gray codes for spanning trees of complete graphs in constant amortized time. In Proceedings of the 2026 Annual ACM-SIAM Symposium on Discrete Algorithms (SODA), pages 833–870. SIAM, 2026.
- [34] Arturo Merino, Torsten Mutze, and Aaron Williams. All your bases are belong to us: listing all bases of a matroid by greedy exchanges. In 11th International Conference on Fun with Algorithms (FUN 2022), volume 226, page 22. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2022.
- [35] Torsten Mütze. Combinatorial Gray codes-an updated survey. arXiv preprint arXiv:2202.01280, 2022. arXiv:2202.01280.
- [36] Torsten Mutze. Combinatorial Gray codes: an updated survey. Electronic Journal of Combinatorics, 30(3), 2023.
- [37] Torsten Mutze. Combinatorial Gray codes: an updated survey. Electronic Journal of Combinatorics, DS26, 2024.
- [38] OEIS Foundation Inc. The On-Line Encyclopedia of Integer Sequences, 2026.
- [39] Vincent Pilaud and Aaron Williams. Skipping ropes: An efficient Gray code algorithm for generating wiggly permutations. In 19th International Symposium on Algorithms and Data Structures (WADS 2025), pages 46–1. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2025.
- [40] Yuan Qiu and Aaron Williams. Generating signed permutations by twisting two-sided ribbons. In Latin American Symposium on Theoretical Informatics, pages 114–129. Springer, 2024. doi:10.1007/978-3-031-55598-5_8.
- [41] Frank Ruskey. Combinatorial generation. Working version (1j-CSC 425/520), 2003.
- [42] Frank Ruskey, Joe Sawada, and Aaron Williams. Binary bubble languages and cool-lex order. Journal of Combinatorial Theory, Series A, 119(1):155–169, 2012. doi:10.1016/J.JCTA.2011.07.005.
- [43] Frank Ruskey and Aaron Williams. Generating balanced parentheses and binary trees by prefix shifts. In Proceedings of the fourteenth symposium on Computing: the Australasian theory, volume 77, pages 107–115, 2008. URL: http://crpit.scem.westernsydney.edu.au/abstracts/CRPITV77Ruskey.html.
- [44] Ahmad Sabri and Vincent Vajnovszki. Restricted steinhaus-johnson-trotter list. In ICGT, 2014.
- [45] Carla Savage. A survey of combinatorial Gray codes. SIAM review, 39(4):605–629, 1997. doi:10.1137/S0036144595295272.
- [46] Joe Sawada and Aaron Williams. Efficient oracles for generating binary bubble languages. the electronic journal of combinatorics, pages P42–P42, 2012.
- [47] Joe Sawada, Aaron Williams, and Dennis Wong. Inside the binary reflected Gray code: Flip-swap languages in 2-Gray code order. In International Conference on Combinatorics on Words, pages 172–184. Springer, 2021. doi:10.1007/978-3-030-85088-3_15.
- [48] Joe Sawada, Aaron Williams, and Dennis Wong. Flip-swap languages in binary reflected Gray code order. Theoretical Computer Science, 933:138–148, 2022. doi:10.1016/J.TCS.2022.08.024.
- [49] Joe Sawada and Dennis Chi-Him Wong. A fast algorithm to generate Beckett-Gray codes. Electronic Notes in Discrete Mathematics, 29:571–577, 2007.
- [50] Matthew B Squire. Gray codes for -free strings. The Electronic Journal of Combinatorics, pages R17–R17, 1996.
- [51] Richard P Stanley. Catalan numbers. Cambridge University Press, 2015.
- [52] Fabian Stedman. Campanalogia: or The art of ringing improved. Good Press, 2025.
- [53] Hugo Steinhaus. One hundred problems in elementary mathematics. Courier Corporation, 1963.
- [54] George Robert Stibitz. Binary counter. United States Patent Number 2307868, 1943.
- [55] Stephen M Stigler. Stigler’s law of eponymy. Transactions of the New York academy of sciences, 39(1 Series II):147–157, 1980.
- [56] Hale F Trotter. Algorithm 115: perm. Communications of the ACM, 5(8):434–435, 1962. doi:10.1145/368637.368660.
- [57] Vincent Vajnovszki and Dennis Wong. Greedy Gray codes for Dyck words and ballot sequences. In International Computing and Combinatorics Conference, pages 29–40. Springer, 2023. doi:10.1007/978-3-031-49193-1_3.
- [58] Herbert S Wilf. A unified setting for sequencing, ranking, and selection algorithms for combinatorial objects. Advances in Mathematics, 24(3):281–291, 1977.
- [59] Aaron Williams. The greedy Gray code algorithm. In Workshop on Algorithms and Data Structures, pages 525–536. Springer, 2013. doi:10.1007/978-3-642-40104-6_46.
- [60] Dennis Chi-Him Wong. Fast algorithms to generate Beckett-Gray codes and coil-in-the-box codes. PhD thesis, University of Guelph, 2007.
