# How to generalize the Thue-Morse sequence to more than two symbols?

The Thue-Morse sequence is defined as a binary sequence and can be generated like

0, 01, 01 10, 01 10 10 01, 01 10 10 01 10 01 01 10, ... .

So the second half of the series is always the binary complement of the first half of the series.

But is there a way to generate an analogous ternary sequence? Intuitively my first guess for a ternary Thue-Morse sequence was like

0, 01, 012, 012 120, 012 120 120 201, 012 120 120 201 120 201 201 012, ...

So here the second half of the series is the "ternary complement" (rotation 0->1, 1->2, 2->0 instead of 0->1, 1->0) of the first half.

But it could also be

0, 01, 012, 012 120 201, 012 120 201 120 201 012 201 012 120, ...

Here the second third is the "ternary complement" of the first third and the third third is the "ternary complement" of the second third.

Does any of my constructions for a ternary Thue-Morse series make sense? Is there maybe a unique way to generate an analogous ternary sequence? And how to construct n-ary versions of the Thue-Morse series in general?

-

It's $0, 012, 012 120 201, 012 120 201 120 201 012 201 012 120, ...$

To easily construct the Thue-Morse sequence, you can do the following: Start with 0 Replace $(0\to01)$ and $(1\to10)$. So you get: $$0, 0 1, 01 10, 0110 1001, 01101001 10010110,...$$

To easily construct the ternary version of Thue-Morse sequence, you can do the following: Start with 0 Replace $(0\to012), (1\to120), (2\to201).$ So you get: $$0, 0 1 2, 012 120 201, 012120201 120201012 201012120$$

To easily construct the $4$-ary version of Thue-Morse sequence, you can do the following: Start with 0 Replace $(0\to 0123), (1\to 1230), (2\to2301), (3\to3012).$ So you get: $$0, 0 1 2 3, 0123 1230 2301 3012, 0123123023013012 1230230130120123 2301301201231230 3012012312302301$$

To easily construct the $n$-ary version of Thue-Morse sequence, you can do the following: Start with $0$ Replace $$(0\to012\dots[n-2][n-1]n), (1\to123\dots[n-1]n0), (2\to234...n01),\dots(n\to n01\dots[n-3][n-2][n-1]).$$

-

To find out a particular number n for b symbols, convert the number to base b, sum the digits, and then modulo by b.

For example, you have two symbols, 0 and 1, and you want to find out what the fifteenth place in this sequence would be. Convert 14 (14 is the fifteenth number since this sequence starts with 0) to base two (the number of symbols), it's 1110. Sum the digits, it's 3. Take 3 modulo two (again, the number of symbols). It's 1. So the fifteenth place in the binary Thue-Morse sequence is 1.

If you have four symbols, 0 and 1 and 2 and 3, and you want to find out the twelfth place in the sequence. Convert 11 to base four, it's 23. Sum the digits, it's 5. Five modulo four is 1, so the twelfth place in the quaternary Thue-Morse sequence is also 1.

I figured that out today. Maybe it was already well-known, I don't know. Hopefully I figured out something new.

Here's how I went about figuring out why it works (and, just as importantly, that it works):

I first heard of the Thue-Morse sequence today. (I was looking for turn-order sequences for card games.)
I read about the "amount of ones being odd or even" thing in the case of two symbols. I.e. 14 in binary is 1110, it has three ones, three is odd, so it's the second symbol ("1").

I searched for whether or not there was a version that worked with more symbols (I figured I wanted to know for games with more players). I found this page, saw MLE's answer and thought that I'd convert the numbers to base three and four respectively, and take a look at the digits, and see if I could find some correspondence. Putting "count the ones" aside for a moment, thinking "is there some digit I can count, some other thing that seems to match with the sequences MLE posted?" and I saw that the sum of the digits modulo the base seemed to match up. I thought about that and saw that that's also another way of expressing Conway's "count the ones". Counting ones and ignoring zeroes is the same as summing the ones and the zeroes. So I was feeling good about it, but I wanted to test.

I wrote a little computer program to do it quickly for me, to convert to a particular base, then sum the digits and then mod the sum. It matched up with everything I threw at it, but I didn't think that was particularly convincing since I only had bases 2, 3 and 4, and numbers up to like 32 or so to work with. I wanted to compare with higher numbers and compare more automatically (which still wouldn't be a proof, but would convince me that it was an approach to explore further). But instead of doing the "substitute 0 for 012" etc approach, I wanted to program some algorithm to quickly find a particular Thue-Morse number. I knew about

T(2n) = T(n)
T(2n+1) = not T(n)


I saw that

T(2n+1) = not T(n) = (T(n)+1) modulo 2.


and thought that one possible generalization for other numbers of symbols could be:

T(bn) = T(n)
T(bn+y) = (T(n)+y) modulo b


where y is lower than b. I don't know if that was already known or not, that was something I came up with.

I implemented this is Scheme as:

(define (thumo n b)
(if (> b n)
n
(modulo
(+
(thumo (quotient n b) b)
(remainder n b))
b)))


I immediately saw that that was pretty much the same as my sum the digits and mod approach:

(define (enarize n b)
;; the word "enarize" I made up but it's a sort
;; of decimal representation of another base.
(if (> b n)
n
(+ ( * 10 (enarize (quotient n b) b)) (remainder n b))))

(define (sum-of-digits n)
(if (> 10 n) n
(+ (sum-of-digits (quotient n 10)) (remainder n 10))))

(define (thumo n b)
(modulo (sum-of-digits (enarize n b)) b))


Because it can be refactored into the same thing. First, I see that I can combine enarize and sum-of-digits because they're so similar (though inversed); I don't need to multiply by ten only to divide by ten again (at least as long as I work in the same "mod") :

(define (sum-of-enarized-digits n b)
(if (> b n)
n
(+ (sum-of-enarized-digits (quotient n b) b) (remainder n b))))


That makes it so that (sum-of-enarized-digits n b) matches up with (sum-of-digits (enarize n b)), and I can wrap it all with a big modulo b to make it match up with the sum-the-digits version of thumo.

But, I also know that when you're planning to mod a sum, it's harmless to mod the operands beforehand (as long as you don't skimp on the final modulo operation). I don't know how to express that formally... it's just something I've figured out when programming. But hopefully it's well known among you math folks. That means I can have the modulo within the recursing function without harm to the final sum:

(define (thumo n b)
(if (> b n)
n
(modulo
(+
(thumo (quotient n b) b)
(remainder n b))
b)))


And, that matches up symbol to symbol with the other algorithm from earlier so QED♥!

PS. I'm new to writing about math, I've got more of a CS background. I'm pretty thin-skinned, I want to do more math stuff so please don't be too harsh on me.

-