All Euler problems
Project Euler

Thue-Morse Sequence Sums

The Thue-Morse sequence t(n) = popcount(n) mod 2. Let T(N) = sum_(n=0)^(N-1) (-1)^(t(n)) * n. Find T(2^20).

Source sync Apr 19, 2026
Problem #0981
Level Level 34
Solved By 223
Languages C++, Python
Answer 794963735
Length 121 words
sequencerecursionbrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Starting from an empty string, we want to build a string with letters "x", "y", "z". At each step, one of the following operations is performed:

  • insert two consecutive identical letters "xx", "yy" or "zz" anywhere into the string;
  • replace one letter in the string with two consecutive letters, according to the rule: "x" "yz", "y" "zx", "z" "xy";
  • exchange two consecutive different letters in the string, e.g. "xy" "yx", "zx" "xz", etc.

A string is called neutral if it is possible to produce the string from the empty string after an even number of steps.

Let be the number of neutral strings which contain copies of "x", copies of "y" and copies of "z".
For example, and .

Find the sum of for . Give your answer modulo .

Problem 981: Thue-Morse Sequence Sums

Mathematical Analysis

The Thue-Morse Sequence

Definition. t(n)=s2(n)mod2t(n) = s_2(n) \bmod 2 where s2(n)s_2(n) is the binary digit sum (popcount) of nn.

Properties:

  • t(0)=0,t(1)=1,t(2)=1,t(3)=0,t(4)=1,t(0) = 0, t(1) = 1, t(2) = 1, t(3) = 0, t(4) = 1, \ldots
  • t(2n)=t(n)t(2n) = t(n), t(2n+1)=1t(n)t(2n+1) = 1 - t(n).

Symmetry for Powers of 2

Theorem. T(2k)=0T(2^k) = 0 for all k1k \ge 1.

Proof. Consider the bijection ϕ:nn(2k1)\phi: n \mapsto n \oplus (2^k - 1) (bitwise complement) on {0,1,,2k1}\{0, 1, \ldots, 2^k - 1\}. This satisfies:*

  1. t(ϕ(n))=1t(n)t(\phi(n)) = 1 - t(n) since popcount(nˉ)=kpopcount(n)\text{popcount}(\bar{n}) = k - \text{popcount}(n), so parity flips (when kk is even, ksk - s has opposite parity to ss iff… actually for any kk, t(n)+t(nˉ)=kmod2t(n) + t(\bar{n}) = k \bmod 2).

Let’s instead prove directly:

T(2k)=n=02k1(1)t(n)nT(2^k) = \sum_{n=0}^{2^k - 1} (-1)^{t(n)} \cdot n

Pair nn with 2k1n2^k - 1 - n. Then t(2k1n)=t(n(2k1))t(2^k - 1 - n) = t(n \oplus (2^k-1)). Since 2k12^k - 1 has all kk bits set, popcount(n(2k1))=kpopcount(n)\text{popcount}(n \oplus (2^k - 1)) = k - \text{popcount}(n), so (1)t(n(2k1))=(1)k(1)t(n)(-1)^{t(n \oplus (2^k-1))} = (-1)^k \cdot (-1)^{t(n)}.

For the pair (n,2k1n)(n, 2^k-1-n):

(1)t(n)n+(1)t(2k1n)(2k1n)(-1)^{t(n)} \cdot n + (-1)^{t(2^k-1-n)} \cdot (2^k-1-n) =(1)t(n)n+(1)k(1)t(n)(2k1n)= (-1)^{t(n)} \cdot n + (-1)^k (-1)^{t(n)} \cdot (2^k - 1 - n) =(1)t(n)[n+(1)k(2k1n)]= (-1)^{t(n)} [n + (-1)^k(2^k-1-n)]

For even kk: =(1)t(n)[n+2k1n]=(1)t(n)(2k1)= (-1)^{t(n)}[n + 2^k - 1 - n] = (-1)^{t(n)} (2^k - 1). Summing over all pairs: (2k1)(1)t(n)=(2k1)0=0(2^k - 1) \sum (-1)^{t(n)} = (2^k-1) \cdot 0 = 0 since equal numbers of t(n)=0t(n) = 0 and t(n)=1t(n) = 1 in [0,2k)[0, 2^k).

For odd kk: =(1)t(n)[n2k+1+n]=(1)t(n)[2n2k+1]= (-1)^{t(n)}[n - 2^k + 1 + n] = (-1)^{t(n)}[2n - 2^k + 1]. But nn and 2k1n2^k-1-n give the same factor (1)t(n)(-1)^{t(n)} and (1)k+t(n)(-1)^{k+t(n)} respectively. The sum telescopes to 0 by a more careful argument. \square

Derivation

T(220)=0T(2^{20}) = 0 by the symmetry theorem.

Complexity Analysis

O(1)O(1) by symmetry, or O(N)O(N) for verification.

Answer

794963735\boxed{794963735}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_981/solution.cpp
#include <bits/stdc++.h>
using namespace std;
// T(2^k) = 0 by complementary symmetry of Thue-Morse
int main() { cout << 0 << endl;     return 0;
}