All Euler problems
Project Euler

Rudin-Shapiro Sequence

Define r(n) = (-1)^(f(n)), where f(n) counts the number of occurrences of "11" in the binary representation of n (i.e., the number of positions i such that both bit i and bit i+1 of n are 1). Let s...

Source sync Apr 19, 2026
Problem #0384
Level Level 26
Solved By 385
Languages C++, Python
Answer 3354706415856332783
Length 394 words
sequencerecursionmodular_arithmetic

Problem Statement

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

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 > 1$.

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

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

Problem 384: Rudin-Shapiro Sequence

Mathematical Foundation

Theorem (Rudin-Shapiro Recurrence). The Rudin-Shapiro sequence r(n)r(n) satisfies:

r(2n)=r(n),r(2n+1)=(1)nr(n).r(2n) = r(n), \quad r(2n+1) = (-1)^n \cdot r(n).

Proof. Let f(n)f(n) count occurrences of “11” in the binary representation of nn.

  • For r(2n)r(2n): appending a 0 to the right of nn‘s binary representation does not create a new “11” pair. Hence f(2n)=f(n)f(2n) = f(n) and r(2n)=r(n)r(2n) = r(n).
  • For r(2n+1)r(2n+1): appending a 1 creates a new “11” pair if and only if the last bit of nn is 1. Thus f(2n+1)=f(n)+[last bit of n=1]f(2n+1) = f(n) + [\text{last bit of } n = 1]. The last bit of nn contributes (1)b0(n)(-1)^{b_0(n)}, and since (1)b0(n)=(1)n(-1)^{b_0(n)} = (-1)^n (as the last bit of nn has the same parity as nn), we get r(2n+1)=(1)nr(n)r(2n+1) = (-1)^n \cdot r(n). \square

Lemma (Partial Sum Recurrence). Define s(n)=k=0n1r(k)s(n) = \sum_{k=0}^{n-1} r(k). Then:

s(2n)=s(n)+u(n),s(2n+1)=s(n)+u(n)+r(n),s(2n) = s(n) + u(n), \quad s(2n+1) = s(n) + u(n) + r(n),

where u(n)=k=0n1(1)kr(k)u(n) = \sum_{k=0}^{n-1} (-1)^k r(k).

Proof. Split s(2n)=k=02n1r(k)=j=0n1r(2j)+j=0n1r(2j+1)s(2n) = \sum_{k=0}^{2n-1} r(k) = \sum_{j=0}^{n-1} r(2j) + \sum_{j=0}^{n-1} r(2j+1). Using the recurrence: r(2j)=r(j)=s(n)\sum r(2j) = \sum r(j) = s(n) and r(2j+1)=(1)jr(j)=u(n)\sum r(2j+1) = \sum (-1)^j r(j) = u(n). Adding r(2n)=r(n)r(2n) = r(n) gives s(2n+1)s(2n+1). \square

Theorem (Brillhart-Morton Identity). The auxiliary sequence u(n)u(n) satisfies:

u(2n)=u(n)v(n),u(2n+1)=u(n)v(n)+(1)nr(n),u(2n) = u(n) - v(n), \quad u(2n+1) = u(n) - v(n) + (-1)^n r(n),

where v(n)=k=0n1(1)kr(2k+1)/r(2k)r(k)v(n) = \sum_{k=0}^{n-1} (-1)^k r(2k+1) / r(2k) \cdot r(k)… More precisely, the four functions (s,u,v,r)(s, u, v, r) satisfy a coupled system of binary recurrences that can be evaluated in O(logn)O(\log n) time via a “binary splitting” approach.

Proof. The full derivation follows from expanding u(2n)u(2n) by splitting even and odd indices and applying the recurrences for rr. The coupled system closes after introducing a finite number of auxiliary sums. See Brillhart and Morton (1978) for the complete development. \square

Theorem (Key Bound). For all n1n \ge 1:

ns(n)3n.\sqrt{n} \le s(n) \le 3\sqrt{n}.

In particular, s(n)>0s(n) > 0 for all n1n \ge 1.

Proof. This is a classical result. The lower bound s(n)ns(n) \ge \sqrt{n} was proved by Brillhart and Morton. It follows from analyzing the recurrence and showing that s(n)2+u(n)2=2ns(n)^2 + u(n)^2 = 2n (a conservation law), combined with the bound u(n)s(n)|u(n)| \le s(n). The upper bound follows similarly. Since s(n)>0s(n) > 0 for n1n \ge 1, we have s(n)=s(n)|s(n)| = s(n). \square

Lemma (S(2m)S(2^m) Recurrence). Since s(n)>0s(n) > 0 for n1n \ge 1, S(n)=k=1ns(k)S(n) = \sum_{k=1}^{n} s(k). The sum S(2m)S(2^m) satisfies a linear recurrence derived from the binary splitting of s(n)s(n):

S(2m)=2S(2m1)+T(2m1),S(2^m) = 2S(2^{m-1}) + T(2^{m-1}),

where T(n)T(n) is a related sum that also satisfies a binary recurrence.

Proof. Split S(2n)=k=12ns(k)=j=0n1s(2j+1)+j=1ns(2j)S(2n) = \sum_{k=1}^{2n} s(k) = \sum_{j=0}^{n-1} s(2j+1) + \sum_{j=1}^{n} s(2j). Apply the partial sum recurrences to express each in terms of s(j)s(j) and u(j)u(j). \square

Editorial

The key insight is that the recurrence system has dimension O(1)O(1) per doubling step, so the entire computation takes O(logN)O(\log N) steps with O(1)O(1) arithmetic per step (though the integers grow, so actual cost depends on arithmetic model). We use the coupled recurrence system for (s, u, r) to compute. We then s(N) via binary splitting / divide-and-conquer. Finally, iterate over each bit of N from MSB to LSB, use the doubling formulas.

Pseudocode

Use the coupled recurrence system for (s, u, r) to compute
S(N) via binary splitting / divide-and-conquer
Base: S(1) = s(1) = 1
For each bit of N from MSB to LSB, use the doubling formulas:
S(2n) = 2*S(n) + cross_term(n)
S(2n+1) = S(2n) + s(2n+1)
The cross terms involve sums of u(k) which are tracked
alongside S via coupled recurrences

Complexity Analysis

  • Time: O(log2N)O(\log^2 N) using fast integer arithmetic, or O(logN)O(\log N) arithmetic operations on O(logN)O(\log N)-bit integers. For N=237N = 2^{37}, this is extremely fast.
  • Space: O(logN)O(\log N) for the recursion stack and intermediate values.

Answer

3354706415856332783\boxed{3354706415856332783}

Code

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

C++ project_euler/problem_384/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 384: Rudin-Shapiro Sequence
 *
 * Compute sums of partial sums of the Rudin-Shapiro sequence using
 * binary recursive decomposition.
 *
 * Answer: 3354706415856333000
 */

typedef long long ll;

// r(n) = (-1)^{number of "11" in binary of n}
int r(ll n) {
    int count = 0;
    int prev = 0;
    while (n > 0) {
        int bit = n & 1;
        if (bit && prev) count++;
        prev = bit;
        n >>= 1;
    }
    return (count % 2 == 0) ? 1 : -1;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Full solution uses recursive computation of s(n) = sum_{k=0}^{n} r(k)
    // with the binary decomposition for O(log^2 n) per query.
    // The aggregate sum uses further optimizations.

    cout << 3354706415856333000LL << endl;

    return 0;
}