All Euler problems
Project Euler

Lattice Paths

Starting at the top-left corner of a 20 x 20 grid, how many routes exist to the bottom-right corner if only rightward and downward moves are permitted? Formally: count the number of monotone lattic...

Source sync Apr 19, 2026
Problem #0015
Level Level 00
Solved By 203,551
Languages C++, Python
Answer 137846528820
Length 455 words
geometrysequenceanalytic_math

Problem Statement

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

Starting in the top left corner of a \(2 \times 2\) grid, and only being able to move to the right and down, there are exactly \(6\) routes to the bottom right corner.

PIC

How many such routes are there through a \(20 \times 20\) grid?

Problem 15: Lattice Paths

Mathematical Development

Definitions

Definition 1. A monotone lattice path from (0,0)(0,0) to (m,n)(m,n) is a sequence of m+nm + n unit steps, each either R=(+1,0)R = (+1,0) or D=(0,+1)D = (0,+1), such that exactly mm steps are RR and exactly nn steps are DD.

Definition 2. Let P(i,j)P(i,j) denote the number of monotone lattice paths from (0,0)(0,0) to (i,j)(i,j).

Theorems

Theorem 1 (Closed-form path count). The number of monotone lattice paths from (0,0)(0,0) to (m,n)(m,n) is

P(m,n)=(m+nm)=(m+n)!m!n!.P(m,n) = \binom{m+n}{m} = \frac{(m+n)!}{m!\, n!}.

Proof. A path is a binary string of length m+nm + n over the alphabet {R,D}\{R, D\} containing exactly mm copies of RR. The path is uniquely determined by specifying the positions of the mm right-steps among the m+nm + n total steps. The number of ways to choose these positions is (m+nm)\binom{m+n}{m}. \square

Theorem 2 (Pascal recurrence). The function PP satisfies:

P(i,0)=1i0,P(0,j)=1j0,P(i,0) = 1 \quad \forall\, i \ge 0, \qquad P(0,j) = 1 \quad \forall\, j \ge 0, P(i,j)=P(i1,j)+P(i,j1)for i,j1.P(i,j) = P(i-1,j) + P(i,j-1) \quad \text{for } i, j \ge 1.

Proof. Boundary conditions: There is exactly one path from (0,0)(0,0) to (i,0)(i,0) (all RR-steps) and one path to (0,j)(0,j) (all DD-steps).

Recurrence: Every path to (i,j)(i,j) with i,j1i,j \ge 1 takes its last step from either (i1,j)(i-1,j) (a right-step) or (i,j1)(i,j-1) (a down-step). These two sets of paths are disjoint and exhaustive, so P(i,j)=P(i1,j)+P(i,j1)P(i,j) = P(i-1,j) + P(i,j-1). \square

Lemma 1 (Equivalence with Pascal’s triangle). P(i,j)=(i+ji)P(i,j) = \binom{i+j}{i} for all i,j0i,j \ge 0.

Proof. By strong induction on i+ji + j. The base case i+j=0i + j = 0 gives P(0,0)=1=(00)P(0,0) = 1 = \binom{0}{0}. For boundary cases, P(i,0)=1=(ii)P(i,0) = 1 = \binom{i}{i} and P(0,j)=1=(j0)P(0,j) = 1 = \binom{j}{0}. For i,j1i,j \ge 1, by the inductive hypothesis and Theorem 2:

P(i,j)=P(i1,j)+P(i,j1)=(i1+ji1)+(i+j1i)=(i+ji),P(i,j) = P(i-1,j) + P(i,j-1) = \binom{i-1+j}{i-1} + \binom{i+j-1}{i} = \binom{i+j}{i},

where the last equality is Pascal’s identity: (n1k1)+(n1k)=(nk)\binom{n-1}{k-1} + \binom{n-1}{k} = \binom{n}{k}. \square

Theorem 3 (Exact evaluation via telescoping product). The binomial coefficient (2nn)\binom{2n}{n} can be computed as

(2nn)=k=1nn+kk.\binom{2n}{n} = \prod_{k=1}^{n} \frac{n+k}{k}.

Moreover, the partial products k=1jn+kk=(n+jj)\prod_{k=1}^{j} \frac{n+k}{k} = \binom{n+j}{j} are integers for all 1jn1 \le j \le n.

Proof. We have

k=1nn+kk=(n+1)(n+2)(2n)12n=(2n)!n!n!=(2nn).\prod_{k=1}^{n} \frac{n+k}{k} = \frac{(n+1)(n+2) \cdots (2n)}{1 \cdot 2 \cdots n} = \frac{(2n)!}{n! \cdot n!} = \binom{2n}{n}.

For integrality of partial products: k=1jn+kk=(n+1)(n+2)(n+j)j!=(n+jj)\prod_{k=1}^{j} \frac{n+k}{k} = \frac{(n+1)(n+2)\cdots(n+j)}{j!} = \binom{n+j}{j}, which is an integer since it counts the number of jj-element subsets of an (n+j)(n+j)-element set. \square

Corollary 1. The multiplicative formula computes (2nn)\binom{2n}{n} using only integer arithmetic (no fractions), since each intermediate quotient result(n+k)/k\text{result} \cdot (n+k) / k is exact.

Proof. At step kk, the running product equals (n+kk)(n+k1)!(n+k1)!\binom{n+k}{k} \cdot \frac{(n+k-1)!}{(n+k-1)!}… more directly: after multiplying by (n+k)(n+k) and dividing by kk, the result equals (n+kk)\binom{n+k}{k}, which is an integer by Theorem 3. \square

Numerical Evaluation

(4020)=40!(20!)2=137846528820.\binom{40}{20} = \frac{40!}{(20!)^2} = 137\,846\,528\,820.

Editorial

We compute the central binomial coefficient multiplicatively instead of expanding full factorials. The loop traverses k=1,,nk = 1, \ldots, n and multiplies the accumulator by (n+k)/k(n+k)/k at each step, which stays integral by the binomial-coefficient derivation. This is sufficient because the resulting product is exactly (2nn)\binom{2n}{n}, the number of lattice paths.

Correctness. By Theorem 3, this computes k=1nn+kk=(2nn)\prod_{k=1}^{n} \frac{n+k}{k} = \binom{2n}{n}. By Corollary 1, all divisions are exact.

Pseudocode

Algorithm: Central Binomial Coefficient by Multiplicative Update
Require: An integer n ≥ 0.
Ensure: The number of lattice paths through an n x n grid.
1: Initialize P ← 1.
2: For each k in {1, 2, ..., n} do:
3:     Update P ← P · (n + k) / k.
4: Return P.

Complexity Analysis

Proposition. The multiplicative formula runs in O(n)O(n) time and O(1)O(1) auxiliary space (assuming O(1)O(1)-cost arithmetic on integers of size O(log(2nn))=O(n)O(\log \binom{2n}{n}) = O(n) bits).

Proof. The loop executes nn iterations, each performing one multiplication and one division. The result (2nn)\binom{2n}{n} has Θ(n)\Theta(n) bits by Stirling’s approximation:

log2(2nn)=2n12log2(πn)+O(1/n)2n.\log_2 \binom{2n}{n} = 2n - \frac{1}{2}\log_2(\pi n) + O(1/n) \approx 2n.

If arbitrary-precision arithmetic is used, each multiplication costs O(n)O(n) bit operations, giving O(n2)O(n^2) total. With machine-word arithmetic (valid for n=20n = 20 since (4020)1.38×1011<263\binom{40}{20} \approx 1.38 \times 10^{11} < 2^{63}), the cost is O(n)O(n). \square

Remark. The dynamic programming approach (Theorem 2) requires O(n2)O(n^2) time and O(n)O(n) space (using row-by-row computation).

Answer

137846528820\boxed{137846528820}

Code

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

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

int main() {
    // Compute C(40, 20) via the telescoping product (Theorem 3).
    // Each intermediate result * (20+k) / k is exact (Corollary 1).
    long long result = 1;
    for (int k = 1; k <= 20; k++)
        result = result * (20 + k) / k;
    cout << result << endl;
    return 0;
}