From 74d5715d00f70b3b4a712ae1c5fa4364717436ea Mon Sep 17 00:00:00 2001 From: Waddah Date: Thu, 15 Oct 2020 20:37:32 +0300 Subject: [PATCH 1/5] add a solution for Project Euler problem #005 --- Project-Euler/Problem005.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Project-Euler/Problem005.js diff --git a/Project-Euler/Problem005.js b/Project-Euler/Problem005.js new file mode 100644 index 0000000000..0ba30669a5 --- /dev/null +++ b/Project-Euler/Problem005.js @@ -0,0 +1,22 @@ +/* +Smallest multiple + +2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. +What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? +*/ + +const findSmallestMultiple = () => { + const divisors = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2] + let num = 21 + let result + + while (!result) { + const isDivisibleByAll = divisors.every((divisor) => num % divisor === 0) + if (isDivisibleByAll) result = num + else num++ + } + + return result +} + +console.log(findSmallestMultiple()) From 03d3720c260072436d9d128cbf1cec7a99fd7abb Mon Sep 17 00:00:00 2001 From: Waddah Date: Thu, 15 Oct 2020 21:33:16 +0300 Subject: [PATCH 2/5] add a solution for Project Euler problem #006 --- Project-Euler/Problem006.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Project-Euler/Problem006.js diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js new file mode 100644 index 0000000000..181ed6be5c --- /dev/null +++ b/Project-Euler/Problem006.js @@ -0,0 +1,24 @@ +/* +Sum square difference +The sum of the squares of the first ten natural numbers is 385, + +The square of the sum of the first ten natural numbers is 3025, + +Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640. + +Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. +*/ + +const findSumSquareDifference = (elementsCount) => { + const naturalNumbers = Array(elementsCount).fill().map((_, i) => i + 1) + + let sumOfSquares = 0 + naturalNumbers.forEach(number => { sumOfSquares += Math.pow(number, 2) }) + + const sumOfNumbers = naturalNumbers.reduce((prev, current) => { current += prev }, 0) + const squareOfSums = Math.pow(sumOfNumbers, 2) + + return squareOfSums - sumOfSquares +} + +console.log(findSumSquareDifference(20)) From b26baef31ed8956098fe3d65bf0636895fb571b4 Mon Sep 17 00:00:00 2001 From: Waddah Date: Sat, 17 Oct 2020 23:11:26 +0300 Subject: [PATCH 3/5] remove files to merge from seprate brnches --- Project-Euler/Problem005.js | 22 ---------------------- Project-Euler/Problem006.js | 24 ------------------------ 2 files changed, 46 deletions(-) delete mode 100644 Project-Euler/Problem005.js delete mode 100644 Project-Euler/Problem006.js diff --git a/Project-Euler/Problem005.js b/Project-Euler/Problem005.js deleted file mode 100644 index 0ba30669a5..0000000000 --- a/Project-Euler/Problem005.js +++ /dev/null @@ -1,22 +0,0 @@ -/* -Smallest multiple - -2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. -What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? -*/ - -const findSmallestMultiple = () => { - const divisors = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2] - let num = 21 - let result - - while (!result) { - const isDivisibleByAll = divisors.every((divisor) => num % divisor === 0) - if (isDivisibleByAll) result = num - else num++ - } - - return result -} - -console.log(findSmallestMultiple()) diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js deleted file mode 100644 index 181ed6be5c..0000000000 --- a/Project-Euler/Problem006.js +++ /dev/null @@ -1,24 +0,0 @@ -/* -Sum square difference -The sum of the squares of the first ten natural numbers is 385, - -The square of the sum of the first ten natural numbers is 3025, - -Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640. - -Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. -*/ - -const findSumSquareDifference = (elementsCount) => { - const naturalNumbers = Array(elementsCount).fill().map((_, i) => i + 1) - - let sumOfSquares = 0 - naturalNumbers.forEach(number => { sumOfSquares += Math.pow(number, 2) }) - - const sumOfNumbers = naturalNumbers.reduce((prev, current) => { current += prev }, 0) - const squareOfSums = Math.pow(sumOfNumbers, 2) - - return squareOfSums - sumOfSquares -} - -console.log(findSumSquareDifference(20)) From c0cd0439cd48906f900f46f972a4cdfd74036853 Mon Sep 17 00:00:00 2001 From: Waddah Date: Sat, 17 Oct 2020 23:29:46 +0300 Subject: [PATCH 4/5] add solution for Project Euler 009 --- Project-Euler/Problem009.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Project-Euler/Problem009.js diff --git a/Project-Euler/Problem009.js b/Project-Euler/Problem009.js new file mode 100644 index 0000000000..a877c89d86 --- /dev/null +++ b/Project-Euler/Problem009.js @@ -0,0 +1,27 @@ +/* +Special Pythagorean triplet + +A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + +a^2 + b^2 = c^2 +For example, 32 + 42 = 9 + 16 = 25 = 52. + +There exists exactly one Pythagorean triplet for which a + b + c = 1000. +Find the product abc. +*/ + +const isPythagoreanTriplet = (a, b, c) => Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2) + +const findSpecialPythagoreanTriplet = () => { + for (let a = 0; a < 1000; a++) { + for (let b = a + 1; b < 1000; b++) { + for (let c = b + 1; c < 1000; c++) { + if (isPythagoreanTriplet(a, b, c) && a + b + c === 1000) { + return a * b * c + } + } + } + } +} + +console.log(findSpecialPythagoreanTriplet()) From 115ca0f1c2e5333eb2429713db3bd5baa0b27d15 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sat, 13 Feb 2021 21:52:01 +0530 Subject: [PATCH 5/5] Rename Problem009.js to Problem9.js --- Project-Euler/{Problem009.js => Problem9.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Project-Euler/{Problem009.js => Problem9.js} (100%) diff --git a/Project-Euler/Problem009.js b/Project-Euler/Problem9.js similarity index 100% rename from Project-Euler/Problem009.js rename to Project-Euler/Problem9.js