mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
chore: Merge pull request #631 from anna-vodimed/master
Add solution for Project-Euler Problem15
This commit is contained in:
17
Project-Euler/Problem015.js
Normal file
17
Project-Euler/Problem015.js
Normal file
@ -0,0 +1,17 @@
|
||||
// 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?
|
||||
*/
|
||||
|
||||
// A lattice path is composed of horizontal and vertical lines that pass through lattice points.
|
||||
|
||||
const latticePath = (gridSize) => {
|
||||
let paths
|
||||
for (let i = 1, paths = 1; i <= gridSize; i++) {
|
||||
paths = paths * (gridSize + i) / i
|
||||
}
|
||||
// The total number of paths can be found using the binomial coefficient (b+a)/a.
|
||||
return paths
|
||||
}
|
||||
console.log(latticePath(20)) // output = 137846528820
|
Reference in New Issue
Block a user