# Thue–Morse sequence

In mathematics, the Thue–Morse or Prouhet–Thue–Morse sequence is the binary sequence (an infinite sequence of 0s and 1s) that can be obtained by starting with 0 and successively appending the Boolean complement of the sequence obtained thus far. It is sometimes called the fair share sequence because of its applications to fair division or parity sequence. The first few steps of this procedure yield the strings 0, 01, 0110, 01101001, 0110100110010110, and so on, which are the prefixes of the Thue–Morse sequence. The full sequence begins:
    01101001100101101001011001101001....
The sequence is named after Axel Thue, Marston Morse and (in its extended form) Eugène Prouhet.

## Definition

There are several equivalent ways of defining the Thue–Morse sequence.

### Direct definition

To compute the nth element t$_n$, write the number n in binary. If the number of ones in this binary expansion is odd then t$_n$ = 1, if even then t$_n$ = 0. That is, t$_n$ is the even parity bit for n. John H. Conway et al. deemed numbers n satisfying t$_n$ = 1 to be odious (intended to be similar to odd) numbers, and numbers for which t$_n$ = 0 to be evil (similar to even) numbers.

### Fast sequence generation

This method leads to a fast method for computing the Thue–Morse sequence: start with t$_0$ = 0, and then, for each n, find the highest-order bit in the binary representation of n that is different from the same bit in the representation of n − 1. If this bit is at an even index, t$_n$ differs from t$_{n−1}$, and otherwise it is the same as t$_{n−1}$.
In Python:
The resulting algorithm takes constant time to generate each sequence element, using only a logarithmic number of bits (constant number of words) of memory.

### Recurrence relation

The Thue–Morse sequence is the sequence t$_n$ satisfying the recurrence relation
    ${\begin{aligned}t_{0}&=0,\\t_{2n}&=t_{n},\\t_{2n+1}&=1-t_{n},\end{aligned}}$
for all non-negative integers n.

### L-system

The Thue–Morse sequence is a morphic word: it is the output of the following Lindenmayer system:
| Variables | 0, 1 |
| Constants | None |
| Start | 0 |
| Rules | (0 → 01), (1 → 10) |

### Characterization using bitwise negation

The Thue–Morse sequence in the form given above, as a sequence of bits, can be defined recursively using the operation of bitwise negation. So, the first element is 0. Then once the first 2$^n$ elements have been specified, forming a string s, then the next 2$^n$ elements must form the bitwise negation of s. Now we have defined the first 2$^{n+1}$ elements, and we recurse.
Spelling out the first few steps in detail:
* We start with 0.
* The bitwise negation of 0 is 1.
* Combining these, the first 2 elements are 01.
* The bitwise negation of 01 is 10.
* Combining these, the first 4 elements are 0110.
* The bitwise negation of 0110 is 1001.
* Combining these, the first 8 elements are 01101001.
* And so on.
So
* T$_0$ = 0.
* T$_1$ = 01.
* T$_2$ = 0110.
* T$_3$ = 01101001.
* T$_4$ = 0110100110010110.
* T$_5$ = 01101001100101101001011001101001.
* T$_6$ = 0110100110010110100101100110100110010110011010010110100110010110.
* And so on.
In Python:
Which can then be converted to a (reversed) string as follows:

### Infinite product

The sequence can also be defined by:
    $\prod _{i=0}^{\infty }\left(1-x^{2^{i}}\right)=\sum _{j=0}^{\infty }(-1)^{t_{j}}x^{j},$
where t$_j$ is the jth element if we start at j = 0.

## Properties

The Thue–Morse sequence contains many squares: instances of the string $XX$, where $X$ denotes the string $A$, ${\overline {A}}$, $A{\overline {A}}A$, or ${\overline {A}}A{\overline {A}}$, where $A=T_{k}$ for some $k\geq 0$ and ${\overline {A}}$ is the bitwise negation of $A$. For instance, if $k=0$, then $A=T_{0}=0$. The square $A{\overline {A}}AA{\overline {A}}A=010010$ appears in $T$ starting at the 16th bit. Since all squares in $T$ are obtained by repeating one of these 4 strings, they all have length $2^{n}$ or $3\cdot 2^{n}$ for some $n\geq 0$. $T$ contains no cubes: instances of $XXX$. There are also no overlapping squares: instances of $0X0X0$ or $1X1X1$. The critical exponent of $T$ is 2.
The Thue–Morse sequence is a uniformly recurrent word: given any finite string X in the sequence, there is some length n$_X$ (often much longer than the length of X) such that X appears in every block of length n$_X$. Notably, the Thue-Morse sequence is uniformly recurrent without being either periodic or eventually periodic (i.e., periodic after some initial nonperiodic segment).
The sequence T$_{2n}$ is a palindrome for any n. Furthermore, let q$_n$ be a word obtained by counting the ones between consecutive zeros in T$_{2n}$ . For instance, q$_1$ = 2 and q$_2$ = 2102012. Since T$_n$ does not contain overlapping squares, the words q$_n$ are palindromic squarefree words.
The Thue–Morse morphism μ is defined on alphabet {0,1} by the substitution map μ(0) = 01, μ(1) = 10: every 0 in a sequence is replaced with 01 and every 1 with 10. If T is the Thue–Morse sequence, then μ(T) is also T. Thus, T is a fixed point of μ. The morphism μ is a prolongable morphism on the free monoid {0,1}$^∗$ with T as fixed point: T is essentially the only fixed point of μ; the only other fixed point is the bitwise negation of T, which is simply the Thue–Morse sequence on (1,0) instead of on (0,1). This property may be generalized to the concept of an automatic sequence.
The generating series of T over the binary field is the formal power series
    $t(Z)=\sum _{n=0}^{\infty }T(n)Z^{n}\ .$
This power series is algebraic over the field of rational functions, satisfying the equation
    $Z+(1+Z)^{2}t+(1+Z)^{3}t^{2}=0$

### In combinatorial game theory

The set of evil numbers (numbers $n$ with $t_{n}=0$) forms a subspace of the nonnegative integers under nim-addition (bitwise exclusive or). For the game of Kayles, evil nim-values occur for few (finitely many) positions in the game, with all remaining positions having odious nim-values.

### The Prouhet–Tarry–Escott problem

The Prouhet–Tarry–Escott problem can be defined as: given a positive integer N and a non-negative integer k, partition the set S = { 0, 1, ..., N-1 } into two disjoint subsets S$_0$ and S$_1$ that have equal sums of powers up to k, that is:
    $\sum _{x\in S_{0}}x^{i}=\sum _{x\in S_{1}}x^{i}$ for all integers i from 1 to k.
This has a solution if N is a multiple of 2$^{k+1}$, given by:
* S$_0$ consists of the integers n in S for which t$_n$ = 0,
* S$_1$ consists of the integers n in S for which t$_n$ = 1.
For example, for N = 8 and k = 2,
    0 + 3 + 5 + 6 = 1 + 2 + 4 + 7,
    0$^2$ + 3$^2$ + 5$^2$ + 6$^2$ = 1$^2$ + 2$^2$ + 4$^2$ + 7$^2$.
The condition requiring that N be a multiple of 2$^{k+1}$ is not strictly necessary: there are some further cases for which a solution exists. However, it guarantees a stronger property: if the condition is satisfied, then the set of kth powers of any set of N numbers in arithmetic progression can be partitioned into two sets with equal sums. This follows directly from the expansion given by the binomial theorem applied to the binomial representing the nth element of an arithmetic progression.
For generalizations of the Thue–Morse sequence and the Prouhet–Tarry–Escott problem to partitions into more than two parts, see Bolker, Offner, Richman and Zara, "The Prouhet–Tarry–Escott problem and generalized Thue–Morse sequences".

### Fractals and turtle graphics

Using turtle graphics, a curve can be generated if an automaton is programmed with a sequence. When Thue–Morse sequence members are used in order to select program states:
* If t(n) = 0, move ahead by one unit,
* If t(n) = 1, rotate by an angle of π/3 radians (60°)
The resulting curve converges to the Koch curve, a fractal curve of infinite length containing a finite area. This illustrates the fractal nature of the Thue–Morse Sequence.
It is also possible to draw the curve precisely using the following instructions:
* If t(n) = 0, rotate by an angle of π radians (180°),
* If t(n) = 1, move ahead by one unit, then rotate by an angle of π/3 radians.

### Equitable sequencing

In their book on the problem of fair division, Steven Brams and Alan Taylor invoked the Thue–Morse sequence but did not identify it as such. When allocating a contested pile of items between two parties who agree on the items' relative values, Brams and Taylor suggested a method they called balanced alternation, or taking turns taking turns taking turns . . . , as a way to circumvent the favoritism inherent when one party chooses before the other. An example showed how a divorcing couple might reach a fair settlement in the distribution of jointly-owned items. The parties would take turns to be the first chooser at different points in the selection process: Ann chooses one item, then Ben does, then Ben chooses one item, then Ann does.
Lionel Levine and Katherine E. Stange, in their discussion of how to fairly apportion a shared meal such as an Ethiopian dinner, proposed the Thue–Morse sequence as a way to reduce the advantage of moving first. They suggested that “it would be interesting to quantify the intuition that the Thue–Morse order tends to produce a fair outcome.”
Robert Richman addressed this problem, but he too did not identify the Thue–Morse sequence as such at the time of publication. He presented the sequences T$_n$ as step functions on the interval [0,1] and described their relationship to the Walsh and Rademacher functions. He showed that the nth derivative can be expressed in terms of T$_n$. As a consequence, the step function arising from T$_n$ is orthogonal to polynomials of order n − 1. A consequence of this result is that a resource whose value is expressed as a monotonically decreasing continuous function is most fairly allocated using a sequence that converges to Thue–Morse as the function becomes flatter. An example showed how to pour cups of coffee of equal strength from a carafe with a nonlinear concentration gradient, prompting a whimsical article in the popular press.
Joshua Cooper and Aaron Dutle showed why the Thue–Morse order provides a fair outcome for discrete events. They considered the fairest way to stage a Galois duel, in which each of the shooters has equally poor shooting skills. Cooper and Dutle postulated that each dueler would demand a chance to fire as soon as the other's a priori probability of winning exceeded their own. They proved that, as the duelers’ hitting probability approaches zero, the firing sequence converges to the Thue–Morse sequence. In so doing, they demonstrated that the Thue–Morse order produces a fair outcome not only for sequences T$_n$ of length 2$^n$, but for sequences of any length.
Thus the mathematics supports using the Thue–Morse sequence instead of alternating turns when the goal is fairness but earlier turns differ monotonically from later turns in some meaningful quality, whether that quality varies continuously or discretely.
Sports competitions form an important class of equitable sequencing problems, because strict alternation often gives an unfair advantage to one team. Ignacio Palacios-Huerta proposed changing the sequential order to Thue–Morse to improve the ex post fairness of various tournament competitions, such as the kicking sequence of a penalty shoot-out in soccer. He did a set of field experiments with pro players and found that the team kicking first won 60% of games using ABAB (or T$_1$), 54% using ABBA (or T$_2$), and 51% using full Thue–Morse (or T$_n$). As a result, ABBA is undergoing extensive trials in FIFA (European and World Championships) and English Federation professional soccer (EFL Cup). An ABBA serving pattern has also been found to improve the fairness of tennis tie-breaks. In competitive rowing, T$_2$ is the only arrangement of port- and starboard-rowing crew members that eliminates transverse forces (and hence sideways wiggle) on a four-membered coxless racing boat, while T$_3$ is one of only four rigs to avoid wiggle on an eight-membered boat.
Fairness is especially important in player drafts. Many professional sports leagues attempt to achieve competitive parity by giving earlier selections in each round to weaker teams. By contrast, fantasy football leagues have no pre-existing imbalance to correct, so they often use a “snake” draft (forward, backward, etc.; or T$_1$). Ian Allan argued that a “third-round reversal” (forward, backward, backward, forward, etc.; or T$_2$) would be even more fair. Richman suggested that the fairest way for “captain A” and “captain B” to choose sides for a pick-up game of basketball mirrors T$_3$: captain A has the first, fourth, sixth, and seventh choices, while captain B has the second, third, fifth, and eighth choices.

### Hash collisions

The initial 2$^k$ bits of the Thue–Morse sequence are mapped to 0 by a wide class of polynomial hash functions modulo a power of two, which can lead to hash collisions.

### Riemann zeta function

Certain linear combinations of Dirichlet series whose coefficients are terms of the Thue–Morse sequence give rise to identities involving the Riemann Zeta function (Tóth, 2022). For instance:
    ${\begin{aligned}\sum _{n\geq 1}{\frac {5t_{n-1}+3t_{n}}{n^{2}}}&=4\zeta (2)={\frac {2\pi ^{2}}{3}},\\\sum _{n\geq 1}{\frac {9t_{n-1}+7t_{n}}{n^{3}}}&=8\zeta (3),\end{aligned}}$
where $(t_{n})_{n\geq 0}$ is the $n^{\rm {th}}$ term of the Thue-Morse sequence. In fact, for all $s$ with real part greater than $1$, we have
    $(2^{s}+1)\sum _{n\geq 1}{\frac {t_{n-1}}{n^{s}}}+(2^{s}-1)\sum _{n\geq 1}{\frac {t_{n}}{n^{s}}}=2^{s}\zeta (s).$

## History

The Thue–Morse sequence was first studied by Eugène Prouhet [fr] in 1851, who applied it to number theory. However, Prouhet did not mention the sequence explicitly; this was left to Axel Thue in 1906, who used it to found the study of combinatorics on words. The sequence was only brought to worldwide attention with the work of Marston Morse in 1921, when he applied it to differential geometry. The sequence has been discovered independently many times, not always by professional research mathematicians; for example, Max Euwe, a chess grandmaster and mathematics teacher, discovered it in 1929 in an application to chess: by using its cube-free property (see above), he showed how to circumvent the threefold repetition rule aimed at preventing infinitely protracted games by declaring repetition of moves a draw. At the time, consecutive identical board states were required to trigger the rule; the rule was later amended to the same board position reoccurring three times at any point, as the sequence shows that the consecutive criterion can be evaded forever.