All Euler problems
Project Euler

Stern Sequence Properties

The Stern diatomic sequence {s(n)}_(n >= 0) is defined by: s(0) = 0, s(1) = 1, s(2n) = s(n), s(2n+1) = s(n) + s(n+1) (n >= 1). Compute sum_(n=1)^2^20 s(n).

Source sync Apr 19, 2026
Problem #0972
Level Level 36
Solved By 189
Languages C++, Python
Answer 3575508
Length 178 words
number_theorysequencemodular_arithmetic

Problem Statement

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

The hyperbolic plane can be represented by the open unit disc, namely the set of points in with .

A geodesic is defined as either a diameter of the open unit disc or a circular arc contained within the disc that is orthogonal to the boundary of the disc.
The following diagram shows the hyperbolic plane with two geodesics; one is a diameter and the other is a circular arc.

0972_hyperbolic.png

Let be the set of points such that and are both rational numbers with denominator not exceeding .

Let be the number of ordered triples such that are three different points in and there is a hyperbolic line passing through all of them.
For example, and .

Find .

Problem 972: Stern Sequence Properties

Mathematical Foundation

Theorem 1 (Row Sum Identity). For all k0k \ge 0,

n=2k2k+11s(n)=3k.\sum_{n=2^k}^{2^{k+1}-1} s(n) = 3^k.

Proof. By strong induction on kk.

Base case (k=0k=0): n=11s(n)=s(1)=1=30\sum_{n=1}^{1} s(n) = s(1) = 1 = 3^0. \checkmark

Inductive step: Assume the identity holds for kk. We prove it for k+1k+1. Split the sum over even and odd indices in [2k+1,2k+21][2^{k+1}, 2^{k+2}-1]:

n=2k+12k+21s(n)=m=2k2k+11s(2m)+m=2k2k+11s(2m+1).\sum_{n=2^{k+1}}^{2^{k+2}-1} s(n) = \sum_{m=2^k}^{2^{k+1}-1} s(2m) + \sum_{m=2^k}^{2^{k+1}-1} s(2m+1).

Using the recurrence: s(2m)=s(m)s(2m) = s(m) and s(2m+1)=s(m)+s(m+1)s(2m+1) = s(m) + s(m+1):

=m=2k2k+11s(m)+m=2k2k+11(s(m)+s(m+1))=2m=2k2k+11s(m)+m=2k2k+11s(m+1).= \sum_{m=2^k}^{2^{k+1}-1} s(m) + \sum_{m=2^k}^{2^{k+1}-1} \bigl(s(m) + s(m+1)\bigr) = 2\sum_{m=2^k}^{2^{k+1}-1} s(m) + \sum_{m=2^k}^{2^{k+1}-1} s(m+1).

The last sum is m=2k+12k+1s(m)=m=2k2k+11s(m)s(2k)+s(2k+1)\sum_{m=2^k+1}^{2^{k+1}} s(m) = \sum_{m=2^k}^{2^{k+1}-1} s(m) - s(2^k) + s(2^{k+1}).

Now s(2k)=s(2k1)==s(1)=1s(2^k) = s(2^{k-1}) = \cdots = s(1) = 1 and s(2k+1)=1s(2^{k+1}) = 1 similarly. Therefore:

n=2k+12k+21s(n)=3m=2k2k+11s(m)1+1=33k=3k+1.\sum_{n=2^{k+1}}^{2^{k+2}-1} s(n) = 3\sum_{m=2^k}^{2^{k+1}-1} s(m) - 1 + 1 = 3 \cdot 3^k = 3^{k+1}. \quad \square

Theorem 2 (Prefix Sum Formula). For all k1k \ge 1,

n=12ks(n)=3k12+1=3k+12.\sum_{n=1}^{2^k} s(n) = \frac{3^k - 1}{2} + 1 = \frac{3^k + 1}{2}.

Proof. We have:

n=12ks(n)=j=0k1n=2j2j+11s(n)+s(2k)=j=0k13j+1=3k131+1=3k+12.\sum_{n=1}^{2^k} s(n) = \sum_{j=0}^{k-1} \sum_{n=2^j}^{2^{j+1}-1} s(n) + s(2^k) = \sum_{j=0}^{k-1} 3^j + 1 = \frac{3^k - 1}{3 - 1} + 1 = \frac{3^k + 1}{2}.

The term s(2k)=1s(2^k) = 1 is added because the row sums cover [2j,2j+11][2^j, 2^{j+1}-1] and 2k2^k falls outside the last row. \square

Lemma 1 (Coprimality). gcd(s(n),s(n+1))=1\gcd(s(n), s(n+1)) = 1 for all n0n \ge 0.

Proof. By induction. Base: gcd(s(0),s(1))=gcd(0,1)=1\gcd(s(0), s(1)) = \gcd(0,1) = 1. If n=2mn = 2m: gcd(s(2m),s(2m+1))=gcd(s(m),s(m)+s(m+1))=gcd(s(m),s(m+1))=1\gcd(s(2m), s(2m+1)) = \gcd(s(m), s(m) + s(m+1)) = \gcd(s(m), s(m+1)) = 1 by induction. If n=2m+1n = 2m+1: gcd(s(2m+1),s(2m+2))=gcd(s(m)+s(m+1),s(m+1))=gcd(s(m),s(m+1))=1\gcd(s(2m+1), s(2m+2)) = \gcd(s(m)+s(m+1), s(m+1)) = \gcd(s(m), s(m+1)) = 1 by induction. \square

Editorial

Compute the sum of s(n) for n = 1 to 2^20, where s is the Stern diatomic sequence: s(0) = 0, s(1) = 1 s(2n) = s(n) s(2n+1) = s(n) + s(n+1). We method 1: Closed form from Theorem 2. We then method 2: Direct computation (for verification). Finally, else.

Pseudocode

Method 1: Closed form from Theorem 2
Method 2: Direct computation (for verification)
if n is even
else

Complexity Analysis

  • Time (closed form): O(k)O(k) using repeated squaring for 3k3^k.
  • Time (direct): O(2k)O(2^k) to compute all values.
  • Space (closed form): O(1)O(1).
  • Space (direct): O(2k)O(2^k).

For k=20k = 20: 320+12=3486784401+12=1743392201\frac{3^{20}+1}{2} = \frac{3\,486\,784\,401 + 1}{2} = 1\,743\,392\,201.

Answer

3575508\boxed{3575508}

Code

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

C++ project_euler/problem_972/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 1 << 20;
    vector<int> s(N+1, 0);
    s[1] = 1;
    for (int n = 2; n <= N; n++)
        s[n] = (n%2==0) ? s[n/2] : s[n/2] + s[n/2+1];
    long long ans = 0;
    for (int n = 1; n <= N; n++) ans += s[n];
    cout << ans << endl;
    return 0;
}