From 378ab51334ed4d2e49736eb5c457b3b573cca765 Mon Sep 17 00:00:00 2001 From: Abdul Khuddus Date: Wed, 30 Oct 2019 15:33:32 +0530 Subject: [PATCH 1/2] Problem 5 done in JavaScript * Added solution for problem 5 in javascript. The function is called for output in console log. Just run the file in node to see the output. --- .../05.Smallest_Multiple.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 JS/05.Smallest_Multiple/05.Smallest_Multiple.js diff --git a/JS/05.Smallest_Multiple/05.Smallest_Multiple.js b/JS/05.Smallest_Multiple/05.Smallest_Multiple.js new file mode 100644 index 0000000..80ff695 --- /dev/null +++ b/JS/05.Smallest_Multiple/05.Smallest_Multiple.js @@ -0,0 +1,23 @@ +const lcm = (firstNum, ...remainingNums) => { + if (remainingNums.length === 0) { + throw (new Error("Input for method must contain at least two numbers")); + } + if (remainingNums.length === 1) { + secondNum = remainingNums[0]; + return (firstNum * secondNum) / gcd(firstNum, secondNum); + } + const [nextNum, ...otherNums] = remainingNums; + return lcm(lcm(firstNum, nextNum), ...otherNums); +} +const gcd = (firstNum, secondNum) => { + const biggerNum = firstNum > secondNum ? firstNum : secondNum; + const smallerNum = firstNum > secondNum ? secondNum : firstNum; + let greatestCommonFactor = smallerNum; + while (biggerNum % greatestCommonFactor !== 0 || smallerNum % greatestCommonFactor !== 0) { + greatestCommonFactor--; + } + return greatestCommonFactor; +} +const nums = new Array(20).fill(1); +const firstTwentyNumbers = nums.map((num, index) => index + num); +console.log(lcm(...firstTwentyNumbers)); \ No newline at end of file From b11d6dc24ac3ea8aa645baac0207913c9e84c660 Mon Sep 17 00:00:00 2001 From: Abdul Khuddus Date: Wed, 30 Oct 2019 17:58:49 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ab26076..1349c12 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Happy Contributing! 😃 | 02 | [Even Fibonacci numbers](https://projecteuler.net/problem=2) | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: | | | 03 | [Largest prime factor](https://projecteuler.net/problem=3) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | 04 | [Largest palindrome product](https://projecteuler.net/problem=4) | :white_check_mark: | | | :white_check_mark: | | | :white_check_mark: | | | | | | -| 05 | [Smallest multiple](https://projecteuler.net/problem=5) | :white_check_mark: | | | :white_check_mark: | | | | | :white_check_mark: | | | | +| 05 | [Smallest multiple](https://projecteuler.net/problem=5) | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | | :white_check_mark: | | | | | 06 | [Sum square difference](https://projecteuler.net/problem=6) | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | | | | 07 | [10001st prime](https://projecteuler.net/problem=7) | :white_check_mark: | | | :white_check_mark: | | :white_check_mark: | :white_check_mark: | | | | | | | 08 | [Largest product in a series](https://projecteuler.net/problem=8) | | | | :white_check_mark: | | | | | | | | |