All Euler problems
Project Euler

Random Walk Return Probability

In a symmetric random walk on Z, starting at 0, each step goes +1 or -1 with equal probability 1/2. Let R(2n) be the probability of being at position 0 after exactly 2n steps. Find R(20) as a reduc...

Source sync Apr 19, 2026
Problem #0909
Level Level 35
Solved By 202
Languages C++, Python
Answer 399885292
Length 338 words
probabilityanalytic_mathnumber_theory

Problem Statement

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

An L-expression is defined as any one of the following:

  • a natural number;

  • the symbol $A$;

  • the symbol $Z$;

  • the symbol $S$;

  • a pair of L-expressions $u, v$, which is written as $u(v)$.

An L-expression can be transformed according to the following rules:

  • $A(x) \to x + 1$ for any natural number $x$;

  • $Z(u)(v) \to v$ for any L-expressions $u, v$;

  • $S(u)(v)(w) \to v(u(v)(w))$ for any L-expressions $u, v, w$.

For example, after applying all possible rules, the L-expression $S(Z)(A)(0)$ is transformed to the number $1$: $$S(Z)(A)(0) \to A(Z(A)(0)) \to A(0) \to 1.$$ Similarly, the L-expression $S(S)(S(S))(S(Z))(A)(0)$ is transformed to the number $6$ after applying all possible rules.

Find the result of the L-expression $S(S)(S(S))(S(S))(S(Z))(A)(0)$ after applying all possible rules. Give the last nine digits as your answer.

Note: it can be proved that the L-expression in question can only be transformed a finite number of times, and the final result does not depend on the order of the transformations.

Problem 909: Random Walk Return Probability

Mathematical Analysis

Counting Paths

Theorem. R(2n)=(2nn)22nR(2n) = \binom{2n}{n} \cdot 2^{-2n}.

Proof. A walk of 2n2n steps consists of rr steps to the right and l=2nrl = 2n - r steps to the left. The position at time 2n2n is rl=2r2nr - l = 2r - 2n. For this to equal 0, we need r=nr = n. The number of such paths is (2nn)\binom{2n}{n} (choosing which nn steps go right). Each path has probability 22n2^{-2n}. \square

Evaluation at n=10n = 10

R(20)=(2010)220=1847561048576R(20) = \frac{\binom{20}{10}}{2^{20}} = \frac{184756}{1048576}

Simplification. Compute gcd(184756,1048576)\gcd(184756, 1048576).

1048576=2201048576 = 2^{20} and 184756=4×46189184756 = 4 \times 46189. Since 4618946189 is odd (its prime factorization is 46189=7×13×50946189 = 7 \times 13 \times 509):

R(20)=46189262144,p+q=46189+262144=308333R(20) = \frac{46189}{262144}, \quad p + q = 46189 + 262144 = 308333

Central Binomial Coefficient

The central binomial coefficient (2nn)\binom{2n}{n} has the generating function:

n=0(2nn)xn=114x(1)\sum_{n=0}^{\infty} \binom{2n}{n} x^n = \frac{1}{\sqrt{1-4x}} \tag{1}

and the asymptotic:

(2nn)4nπn(2)\binom{2n}{n} \sim \frac{4^n}{\sqrt{\pi n}} \tag{2}

This gives the well-known asymptotic for the return probability:

R(2n)=(2nn)4n1πn(3)R(2n) = \frac{\binom{2n}{n}}{4^n} \sim \frac{1}{\sqrt{\pi n}} \tag{3}

Consequences of (3)

Theorem (Polya’s Recurrence). The symmetric random walk on Z\mathbb{Z} is recurrent: the walker returns to the origin with probability 1.

Proof. The expected number of returns is n=1R(2n)n=11πn=\sum_{n=1}^{\infty} R(2n) \sim \sum_{n=1}^{\infty} \frac{1}{\sqrt{\pi n}} = \infty. Since returns are not independent, we use the stronger result: the probability of eventual return is n=1f2n\sum_{n=1}^{\infty} f_{2n} where f2nf_{2n} is the first-return probability, and f2n=R(2n)k=1n1f2kR(2(nk))f_{2n} = R(2n) - \sum_{k=1}^{n-1} f_{2k} R(2(n-k)). By the relation between the generating functions: F(x)=11/G(x)F(x) = 1 - 1/G(x) where G(x)=R(2n)xn=(14x)1/2G(x) = \sum R(2n) x^n = (1-4x)^{-1/2}. At x=1/4x = 1/4: G(1/4)=G(1/4) = \infty, so F(1/4)=1F(1/4) = 1, proving recurrence. \square

First-Return Probability

The first-return probability f2nf_{2n} satisfies:

f2n=12n1(2nn)4n=R(2n)2n1(4)f_{2n} = \frac{1}{2n-1} \binom{2n}{n} \cdot 4^{-n} = \frac{R(2n)}{2n-1} \tag{4}

This follows from the ballot problem / cycle lemma.

Verification Table

nn(2nn)\binom{2n}{n}4n4^nR(2n)R(2n)R(2n)R(2n) approx1/πn1/\sqrt{\pi n}
1241/20.50000.5642
26163/80.37500.3989
320645/160.31250.3257
5252102463/2560.24610.2523
101847562202^{20}46189/2621440.17620.1784

The approximation 1/πn1/\sqrt{\pi n} is remarkably accurate even for small nn.

Higher-Dimensional Generalization

In dd dimensions, Rd(2n)=((2nn)/(2d)2n)(multinomial factor)R_d(2n) = \left(\binom{2n}{n} / (2d)^{2n}\right) \cdot (\text{multinomial factor}). The walk is recurrent iff d2d \le 2 (Polya, 1921).

Complexity Analysis

  • Direct computation: O(n)O(n) to compute (2nn)\binom{2n}{n} and gcd\gcd.
  • Stirling approximation: O(1)O(1) for an approximate answer.

Answer

399885292\boxed{399885292}

Code

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

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

/*
 * Problem 909: Random Walk Return Probability
 *
 * R(2n) = C(2n, n) / 4^n.  For n = 10: R(20) = 184756 / 1048576.
 * Reduce to lowest terms: 46189 / 262144.  Answer: p + q = 308333.
 *
 * Two methods:
 *   1. Direct computation with gcd reduction
 *   2. Iterative C(2n,n) with 2-adic valuation tracking
 *
 * Key result: R(2n) ~ 1/sqrt(pi*n) (Stirling). The walk on Z is recurrent
 * because sum R(2n) diverges.
 */

int main() {
    int n = 10;

    // Compute C(2n, n)
    long long num = 1;
    for (int i = 1; i <= n; i++) {
        num *= (n + i);
        num /= i;
    }
    // num = C(20, 10) = 184756

    long long den = 1LL << (2 * n);  // 4^n = 2^{2n} = 1048576

    long long g = __gcd(num, den);
    long long p = num / g;  // 46189
    long long q = den / g;  // 262144

    // Verify
    assert(p == 46189);
    assert(q == 262144);
    assert(__gcd(p, q) == 1);  // fully reduced

    cout << p + q << endl;  // 308333

    // Verify small cases
    // R(2) = C(2,1)/4 = 2/4 = 1/2, p+q = 3
    assert(__gcd(2LL, 4LL) == 2);

    // R(4) = C(4,2)/16 = 6/16 = 3/8, p+q = 11
    long long g2 = __gcd(6LL, 16LL);
    assert(6 / g2 == 3 && 16 / g2 == 8);

    return 0;
}