Project Euler Lab - Problem 384

#384 - Rudin-Shapiro Sequence

● ResearchOfficial difficulty: 69%NetworksTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Define the sequence \(a(n)\) as the number of adjacent pairs of ones in the binary expansion of \(n\) (possibly overlapping).
E.g.: \(a(5) = a(101_2) = 0\), \(a(6) = a(110_2) = 1\), \(a(7) = a(111_2) = 2\).

Define the sequence \(b(n) = (-1)^{a(n)}\).
This sequence is called the Rudin-Shapiro sequence.

Also consider the summatory sequence of \(b(n)\): \(s(n) = \sum \limits_{i = 0}^n b(i)\).

The first couple of values of these sequences are:

\(n\) \(0\) \(1\) \(2\) \(3\) \(4\) \(5\) \(6\) \(7\)
\(a(n)\) \(0\) \(0\) \(0\) \(1\) \(0\) \(0\) \(1\) \(2\)
\(b(n)\) \(1\) \(1\) \(1\) \(-1\) \(1\) \(1\) \(-1\) \(1\)
\(s(n)\) \(1\) \(2\) \(3\) \(2\) \(3\) \(4\) \(3\) \(4\)

The sequence \(s(n)\) has the remarkable property that all elements are positive and every positive integer \(k\) occurs exactly \(k\) times.

Define \(g(t,c)\), with \(1 \le c \le t\), as the index in \(s(n)\) for which \(t\) occurs for the \(c\)'th time in \(s(n)\).
E.g.: \(g(3,3) = 6\), \(g(4,2) = 7\) and \(g(54321,12345) = 1220847710\).

Let \(F(n)\) be the Fibonacci sequence defined by:
\(F(0)=F(1)=1\) and
\(F(n)=F(n-1)+F(n-2)\) for \(n \gt 1\).

Define \(GF(t)=g(F(t),F(t-1))\).

Find \(\sum GF(t)\) for \(2 \le t \le 45\).

This problem is taken from Project Euler, Problem 384.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=384. Published Sunday, 13th May 2012, 02:00 am. Solved by 391 members at time of mirroring.

Why this is useful

Numerical Computing. Matrix methods and linear-recurrence acceleration are the machinery behind covariance work, PCA, and lattice/transition models (Phases 4, 5, 8).

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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.