From db3831401f309d6a8f8c5f424dde7b6a402a168e Mon Sep 17 00:00:00 2001 From: Vodimed <67785939+anna-vodimed@users.noreply.github.com> Date: Sun, 4 Jul 2021 13:53:20 +0000 Subject: [PATCH 1/4] Add solution for Project-Euler Problem15 --- Project-Euler/Problem015.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Project-Euler/Problem015.js diff --git a/Project-Euler/Problem015.js b/Project-Euler/Problem015.js new file mode 100644 index 000000000..f33a48b79 --- /dev/null +++ b/Project-Euler/Problem015.js @@ -0,0 +1,13 @@ +// 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? +*/ + +const latticePath = n => { + + for (var i = 1, c = 1; i <= n; i++) + c = c * (n + i) / i; + return c; +} +console.log(latticePath(20)); From b941672b46201f24dde0f49117c150ba8eaa37a9 Mon Sep 17 00:00:00 2001 From: Vodimed <67785939+anna-vodimed@users.noreply.github.com> Date: Wed, 7 Jul 2021 11:33:53 +0000 Subject: [PATCH 2/4] update problem15 --- Project-Euler/Problem015.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Project-Euler/Problem015.js b/Project-Euler/Problem015.js index f33a48b79..35b1aa807 100644 --- a/Project-Euler/Problem015.js +++ b/Project-Euler/Problem015.js @@ -4,10 +4,13 @@ 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? */ -const latticePath = n => { +//A lattice path is composed of horizontal and vertical lines that pass through lattice points. - for (var i = 1, c = 1; i <= n; i++) - c = c * (n + i) / i; - return c; +const latticePath = (gridSize) => { + + for (var i = 1, paths = 1; i <= gridSize; i++) + //The total number of paths can be found using the binomial coefficient (b+a)/a. + paths = paths * (gridSize + i) / i; + return paths; } -console.log(latticePath(20)); +console.log(latticePath(20)); //output = 137846528820 From 22fcd2d5ee6dba3862a7f6ff5f64c4e6cdc62e62 Mon Sep 17 00:00:00 2001 From: Kim Date: Wed, 7 Jul 2021 19:34:04 +0300 Subject: [PATCH 3/4] code formatted using standard.js --- Project-Euler/Problem015.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Project-Euler/Problem015.js b/Project-Euler/Problem015.js index 35b1aa807..cd20fa204 100644 --- a/Project-Euler/Problem015.js +++ b/Project-Euler/Problem015.js @@ -4,13 +4,12 @@ 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. +// A lattice path is composed of horizontal and vertical lines that pass through lattice points. const latticePath = (gridSize) => { - - for (var i = 1, paths = 1; i <= gridSize; i++) - //The total number of paths can be found using the binomial coefficient (b+a)/a. - paths = paths * (gridSize + i) / i; - return paths; + for (var i = 1, paths = 1; i <= gridSize; i++) + // The total number of paths can be found using the binomial coefficient (b+a)/a. + { paths = paths * (gridSize + i) / i } + return paths } -console.log(latticePath(20)); //output = 137846528820 +console.log(latticePath(20)) // output = 137846528820 From b35104aaa31571b70986d96be6b7212c9356f816 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Wed, 7 Jul 2021 23:01:34 +0530 Subject: [PATCH 4/4] chore: Fix code formatting --- Project-Euler/Problem015.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem015.js b/Project-Euler/Problem015.js index cd20fa204..d35404733 100644 --- a/Project-Euler/Problem015.js +++ b/Project-Euler/Problem015.js @@ -7,9 +7,11 @@ 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) => { - for (var i = 1, paths = 1; i <= gridSize; i++) + 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. - { paths = paths * (gridSize + i) / i } return paths } console.log(latticePath(20)) // output = 137846528820