From 74d5715d00f70b3b4a712ae1c5fa4364717436ea Mon Sep 17 00:00:00 2001 From: Waddah Date: Thu, 15 Oct 2020 20:37:32 +0300 Subject: [PATCH 1/4] 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/4] 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/4] 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 1538f4188e402cdf3f8de95bd4dc9e2ee7d4ad66 Mon Sep 17 00:00:00 2001 From: Waddah Date: Sat, 17 Oct 2020 23:36:20 +0300 Subject: [PATCH 4/4] add solution for Project Euler 020 --- Project-Euler/Problem020.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Project-Euler/Problem020.js diff --git a/Project-Euler/Problem020.js b/Project-Euler/Problem020.js new file mode 100644 index 0000000000..fc92df4f9c --- /dev/null +++ b/Project-Euler/Problem020.js @@ -0,0 +1,21 @@ +/* +Factorial digit sum + +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! +*/ + +const findFactorialDigitSum = (num) => { + let result = 0 + const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false }) + stringifiedNumber.split('').map(num => { result += Number(num) }) + return result +} + +const factorize = (num) => num === 0 ? 1 : num * factorize(num - 1) + +console.log(findFactorialDigitSum(100))