Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #15 medium

Lattice Paths

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

How many such routes are there through a 20×2020 \times 20 grid?

View on Project Euler

Implementations

cpp
// Answer: 137846528820
#include <iostream>
#include <vector>
#include <cstdint>
uint64_t lattice_path(size_t grid_size)
{
std::vector< uint64_t > grid((grid_size+1)*(grid_size+1),1);
for (int x = grid_size-1; 0 <= x ; x--) {
for (int y = grid_size-1; 0 <= y; y--) {
int pos = (y*(grid_size+1))+x;
grid.at(pos) = grid.at(pos+1) + grid.at(pos+(grid_size+1));
}
}
return grid.at(0);
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[])
{
std::cout << "Answer: " << lattice_path(2) << std::endl;
std::cout << "Answer: " << lattice_path(20) << std::endl;
return 0;
}
#endif // #if ! defined UNITTEST_MODE
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

This is a classic combinatorics problem. To navigate an n×n grid with only right and down moves, you need exactly n right moves and n down moves, for a total of 2n moves.

The number of paths is the number of ways to arrange n right moves and n down moves, which is given by the binomial coefficient:

(2nn)=(2n)!(n!)2\binom{2n}{n} = \frac{(2n)!}{(n!)^2}

For a 20×20 grid, this becomes (4020)\binom{40}{20}, which is the number shown in the solutions.

Algorithm Analysis

Most implementations use direct binomial coefficient calculation rather than dynamic programming to avoid overflow and precision issues with large factorials.

  • Direct factorial approach: Compute (2n)! / (n! * n!) but requires big integer arithmetic
  • Iterative multiplication: Compute the coefficient by multiplying terms iteratively to avoid computing full factorials
  • Dynamic programming: Build a grid where each cell represents paths to that point (shown in C++ and Ruby implementations)

Time complexity: O(n) for iterative binomial calculation, O(n²) for dynamic programming grid approach.

Performance Analysis

  • Time Complexity: O(n) for iterative binomial coefficient calculation (n=20 multiplications), O(n²) for DP grid approach.
  • Space Complexity: O(1) for iterative method, O(n²) for DP grid (400 cells for 20×20).
  • Execution Time: Virtually instantaneous for both approaches, suitable for real-time computation.
  • Scalability: Iterative method scales better for larger grids, DP approach becomes memory-intensive.

Key Insights

  • The problem requires exactly n right and n down moves in some order
  • Binomial coefficients grow extremely rapidly - C(40,20) is over 137 trillion
  • Dynamic programming approach demonstrates the concept of optimal substructure
  • The iterative binomial calculation is more memory-efficient than factorial computation

Educational Value

This problem teaches:

  • Fundamental combinatorics and counting principles
  • Binomial coefficients and their interpretation
  • Dynamic programming for path counting problems
  • The importance of choosing appropriate data types for large numbers
  • How mathematical insight can replace brute-force enumeration

Key Insights

  • The problem requires exactly n right and n down moves in some order
  • Binomial coefficients grow extremely rapidly - C(40,20) is over 137 trillion
  • Dynamic programming approach demonstrates the concept of optimal substructure
  • The iterative binomial calculation is more memory-efficient than factorial computation

Educational Value

This problem teaches:

  • Fundamental combinatorics and counting principles
  • Binomial coefficients and their interpretation
  • Dynamic programming for path counting problems
  • The importance of choosing appropriate data types for large numbers
  • How mathematical insight can replace brute-force enumeration