Project Euler Lab - Problem 361

#361 - Subsequence of Thue-Morse Sequence

● ResearchOfficial difficulty: 95%RecurrencesTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

The Thue-Morse sequence \(\{T_n\}\) is a binary sequence satisfying:

  • \(T_0 = 0\)
  • \(T_{2n} = T_n\)
  • \(T_{2n + 1} = 1 - T_n\)

The first several terms of \(\{T_n\}\) are given as follows:
\(01101001{\color{red}10010}1101001011001101001\cdots\)

We define \(\{A_n\}\) as the sorted sequence of integers such that the binary expression of each element appears as a subsequence in \(\{T_n\}\).
For example, the decimal number \(18\) is expressed as \(10010\) in binary. \(10010\) appears in \(\{T_n\}\) (\(T_8\) to \(T_{12}\)), so \(18\) is an element of \(\{A_n\}\).
The decimal number \(14\) is expressed as \(1110\) in binary. \(1110\) never appears in \(\{T_n\}\), so \(14\) is not an element of \(\{A_n\}\).

The first several terms of \(\{A_n\}\) are given as follows:

\(n\) \(0\) \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\) \(8\) \(9\) \(10\) \(11\) \(12\) \(\cdots\)
\(A_n\) \(0\) \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(9\) \(10\) \(11\) \(12\) \(13\) \(18\) \(\cdots\)

We can also verify that \(A_{100} = 3251\) and \(A_{1000} = 80852364498\).

Find the last \(9\) digits of \(\sum \limits_{k = 1}^{18} A_{10^k}\).

This problem is taken from Project Euler, Problem 361.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=361. Published Sunday, 4th December 2011, 04:00 am. Solved by 377 members at time of mirroring.

Why this is useful

Optimization. The transferable skill is replacing infeasible enumeration with a mathematical reduction - the core move in calibration and large-scale computation (Phases 10, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.