From e3b60cbc093aaf1ce8e519c46872144abc2f77e3 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:38 +0100 Subject: [PATCH] feat(euler): 15 - Lattice paths - --- euler/src/15 - Lattice paths.ts | 14 ++++++++++++++ euler/thoughts/15 - Lattice paths.md | 9 +++++++++ 2 files changed, 23 insertions(+) create mode 100644 euler/src/15 - Lattice paths.ts create mode 100644 euler/thoughts/15 - Lattice paths.md diff --git a/euler/src/15 - Lattice paths.ts b/euler/src/15 - Lattice paths.ts new file mode 100644 index 0000000..9ff61e2 --- /dev/null +++ b/euler/src/15 - Lattice paths.ts @@ -0,0 +1,14 @@ +// See https://github.com/newtykins/the-honk/tree/main/euler/thoughts/15%20-Lattice%20Paths.md +export {}; + +const factorial = (n: number) => { + if (n < 0) return -1; + else if (n === 0) return 1; + else return n * factorial(n - 1); +}; + +const countLatticePaths = (width: number, height: number) => { + return factorial(width + height) / (factorial(width) * factorial(height)); +}; + +console.log(countLatticePaths(20, 20)); diff --git a/euler/thoughts/15 - Lattice paths.md b/euler/thoughts/15 - Lattice paths.md new file mode 100644 index 0000000..98fe247 --- /dev/null +++ b/euler/thoughts/15 - Lattice paths.md @@ -0,0 +1,9 @@ +For a lattice with dimensions m x n, all paths will have m + n segments. m segments go down, n segments go right. To count m + n slots, count the ways to pick m downward moves - the rest will be rightward moves. + +![](assets/15.svg) + +### LaTeX + +``` +{m + n \choose m} = {m + n \choose n} = \frac{(m+n)!}{m!n!} +```