eulerbooks/notebooks/problem0015.ipynb

121 lines
3.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "6cc372f3",
"metadata": {},
"source": [
"# [Lattice Paths](https://projecteuler.net/problem=15)\n",
"\n",
"Let $f(m, n)$ equal the number of routes in an $m \\times n$ grid. Intuitively, if you start at the top left corner, you immediately have two choices - you can go down or right. If you go down, there are $f(m-1,n)$ routes in the $(m - 1) \\times n$ subgrid; if you go right, there are $f(m,n-1)$ routes in the $m \\times (n-1)$ subgrid. Therefore\n",
"$$f(m,n) = f(m-1,n) + f(m,n-1)$$\n",
"\n",
"There are two trivial base cases: $f(m,0) = 1$ for any $m$ and $f(0,n) = 1$ for any $n$. From these facts, you can write a simple [memoized](https://en.wikipedia.org/wiki/Memoization) recursive function to solve this problem."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "9878534a",
"metadata": {},
"outputs": [],
"source": [
"from functools import cache\n",
"\n",
"@cache\n",
"def f(m, n):\n",
" if m == 0:\n",
" return 1\n",
" if n == 0:\n",
" return 1\n",
" \n",
" return f(m - 1, n) + f(m, n - 1)"
]
},
{
"cell_type": "markdown",
"id": "5beab0ed",
"metadata": {},
"source": [
"Then the answer is given by"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f21257bc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"137846528820"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f(20,20)"
]
},
{
"cell_type": "markdown",
"id": "3561530b",
"metadata": {},
"source": [
"However, we can do better and give a non-recursive formula for $f$. It turns out, for an $m \\times n$ grid, there are $\\binom{m+n}{m}$ possible routes. Here's an outline of how you can prove this [inductively](https://en.wikipedia.org/wiki/Mathematical_induction):\n",
"\n",
"1. Prove $f(m, 0) = \\binom{m+0}{m} = 1$ for all $m$\n",
" 1. Prove $f(0, 0) = \\binom{0 + 0}{0} = 1$\n",
" 2. Assuming $f(j, 0) = \\binom{j+0}{j} = 1$ for some $j$, prove $f(j+1,0) = \\binom{j+1+0}{j+1} = 1$\n",
"2. Assuming there exists some $k$ such that for all $m$, $f(m, k) = \\binom{m + k}{m}$, prove $f(m, k+1) = \\binom{m + k + 1}{m}$ for all $m$\n",
" 1. Prove $f(0, k+1) = \\binom{0+k+1}{0} = 1$\n",
" 2. Assuming $f(j, k+1) = \\binom{j+k+1}{j}$ for some $j$, prove $f(j+1,k+1) = \\binom{j+k+2}{j+1}$\n",
"\n",
"Note that there are *three* inductive proofs here - an overall induction on $m$ and nested inductions in both the base case and the induction step (yo dawg, I heard you like induction...).\n",
"\n",
"One useful identity for proving this, particularly in step 2, comes from [Pascal's triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle):\n",
"$$\\binom{n}{k} = \\binom{n-1}{k} + \\binom{n-1}{k-1}$$"
]
},
{
"cell_type": "markdown",
"id": "2c6e7d8c",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Central binomial coefficients: [A000984](https://oeis.org/A000984)\n",
"* General formula: [A046899](https://oeis.org/A046899)\n",
"\n",
"#### Copyright (C) 2025 filifa\n",
"\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.5",
"language": "sage",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}