|
| 1 | +// https://projecteuler.net/problem=15 |
| 2 | +/* Starting in the top left corner of a ×ばつ2 grid, and only being able to move to |
| 3 | +the right and down, there are exactly 6 routes to the bottom right corner. |
| 4 | +How many such routes are there through a ×ばつ20 grid? |
| 5 | +*/ |
| 6 | + |
| 7 | +// A lattice path is composed of horizontal and vertical lines that pass through lattice points. |
| 8 | + |
| 9 | +const latticePath = (gridSize) => { |
| 10 | + let paths |
| 11 | + for (let i = 1, paths = 1; i <= gridSize; i++) { |
| 12 | + paths = paths * (gridSize + i) / i |
| 13 | + } |
| 14 | + // The total number of paths can be found using the binomial coefficient (b+a)/a. |
| 15 | + return paths |
| 16 | +} |
| 17 | +console.log(latticePath(20)) // output = 137846528820 |
0 commit comments