2021-07-04 13:53:20 +00:00
|
|
|
|
// https://projecteuler.net/problem=15
|
|
|
|
|
|
/* Starting in the top left corner of a 2×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×20 grid?
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2021-07-07 19:34:04 +03:00
|
|
|
|
// A lattice path is composed of horizontal and vertical lines that pass through lattice points.
|
2021-07-04 13:53:20 +00:00
|
|
|
|
|
2021-10-10 18:18:25 +02:00
|
|
|
|
export const latticePath = (gridSize) => {
|
2021-07-07 23:01:34 +05:30
|
|
|
|
let paths
|
|
|
|
|
|
for (let i = 1, paths = 1; i <= gridSize; i++) {
|
2023-10-03 23:08:19 +02:00
|
|
|
|
paths = (paths * (gridSize + i)) / i
|
2021-07-07 23:01:34 +05:30
|
|
|
|
}
|
2021-07-07 19:34:04 +03:00
|
|
|
|
// The total number of paths can be found using the binomial coefficient (b+a)/a.
|
|
|
|
|
|
return paths
|
2021-07-04 13:53:20 +00:00
|
|
|
|
}
|
2021-10-10 18:18:25 +02:00
|
|
|
|
|
|
|
|
|
|
// > latticePath(20))
|
|
|
|
|
|
// 137846528820
|