All Euler problems
Project Euler

Collatz Stopping Time Statistics

The Collatz stopping time t(n) is the number of steps to reach 1 from n using the rule: if even, n -> n/2; if odd, n -> 3n + 1. Find sum_(n=1)^(10^6) t(n).

Source sync Apr 19, 2026
Problem #0970
Level Level 38
Solved By 127
Languages C++, Python
Answer 44754029
Length 225 words
dynamic_programmingmodular_arithmeticsequence

Problem Statement

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

Starting at zero, a kangaroo hops along the real number line in the positive direction. Each successive hop takes the kangaroo forward a uniformly random distance between and . Let be the expected number of hops needed for the kangaroo to pass on the real line.

For example, . The first eight digits after the decimal point that are different from six are .

Similarly, . Here the first eight digits after the decimal point that are different from six are .

Find and give as your answer the first eight digits after the decimal point that are different from six.

Problem 970: Collatz Stopping Time Statistics

Mathematical Analysis

The Collatz Conjecture

The Collatz conjecture (also known as the 3n+13n+1 conjecture) states that for every positive integer nn, the sequence n,C(n),C(C(n)),n, C(n), C(C(n)), \ldots eventually reaches 1, where C(n)=n/2C(n) = n/2 if nn is even and C(n)=3n+1C(n) = 3n+1 if nn is odd.

Theorem (Tao, 2019). Almost all Collatz orbits attain almost bounded values. Specifically, for any function ff with f(n)f(n) \to \infty, the set {n:mink0C(k)(n)<f(n)}\{n : \min_{k \ge 0} C^{(k)}(n) < f(n)\} has density 1.

The conjecture has been verified computationally for all n<1020n < 10^{20}.

Stopping Time Properties

Definition. t(n)=0t(n) = 0 for n=1n = 1. For n>1n > 1: t(n)=1+t(C(n))t(n) = 1 + t(C(n)).

Theorem. The average stopping time satisfies 1Nn=1Nt(n)clog2N\frac{1}{N}\sum_{n=1}^{N} t(n) \sim c \cdot \log_2 N for some constant cc (empirically c6.95c \approx 6.95).

Heuristically, each odd step multiplies by 3/2\sim 3/2 and each even step divides by 2. Since about 1/31/3 of steps are odd, the expected number of steps to halve nn is about 3, giving t(n)3log2nt(n) \approx 3 \log_2 n.

Memoization

Memoizing t(n)t(n) for nNn \le N avoids recomputation. Values t(C(n))t(C(n)) may involve n>Nn > N (e.g., C(n)=3n+1>NC(n) = 3n+1 > N), but these chain values decrease rapidly.

Derivation

Editorial

Compute the sum of total stopping times t(n) for n = 1 to 10^6. t(n) is the number of steps in the Collatz sequence until reaching 1. We allocate array t[1..N]t[1..N]. We then iterate over n=2,,Nn = 2, \ldots, N: compute t[n]t[n] by following the chain, using cached values when available. Finally, sum all t[n]t[n].

Pseudocode

Allocate array $t[1..N]$
Set $t[1] = 0$
For $n = 2, \ldots, N$: compute $t[n]$ by following the chain, using cached values when available
Sum all $t[n]$

Proof of Correctness

Assuming the Collatz conjecture (verified for n1020n \le 10^{20}), every chain terminates. Memoization correctly stores each value once.

Complexity Analysis

O(NlogN)O(N \log N) amortized time, O(N)O(N) space.

Answer

44754029\boxed{44754029}

Code

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

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