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 bfcf986c6076501a3053f7c83b5026ee980576a6 Mon Sep 17 00:00:00 2001 From: Waddah Date: Sat, 17 Oct 2020 23:33:32 +0300 Subject: [PATCH 4/4] add solution for Project Euler 014 --- Project-Euler/Problem014.js | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Project-Euler/Problem014.js diff --git a/Project-Euler/Problem014.js b/Project-Euler/Problem014.js new file mode 100644 index 0000000000..62a2974ec0 --- /dev/null +++ b/Project-Euler/Problem014.js @@ -0,0 +1,47 @@ +/* +Longest Collatz sequence + +The following iterative sequence is defined for the set of positive integers: + +n → n/2 (n is even) +n → 3n + 1 (n is odd) + +Using the rule above and starting with 13, we generate the following sequence: + +13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 +It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. + +Which starting number, under one million, produces the longest chain? + +NOTE: Once the chain starts the terms are allowed to go above one million. +*/ + +const getCollatzSequenceLength = (num, seqLength) => { + if (num === 1) { + return seqLength + } else { + let newElement + if (num % 2 === 0) { + newElement = num / 2 + } else { + newElement = (3 * num) + 1 + } + seqLength++ + return getCollatzSequenceLength(newElement, seqLength) + } +} + +const findLongestCollatzSequence = () => { + let startingPointForLargestSequence = 1 + let largestSequnceLength = 1 + for (let i = 2; i < 1000000; i++) { + const currentSequenceLength = getCollatzSequenceLength(i, 1) + if (currentSequenceLength > largestSequnceLength) { + startingPointForLargestSequence = i + largestSequnceLength = currentSequenceLength + } + } + return startingPointForLargestSequence +} + +console.log(findLongestCollatzSequence())