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 diff --git a/README.md b/README.md index 8875d6a..1349c12 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ Happy Contributing! 😃 | 01 | [Multiples of 3 and 5](https://projecteuler.net/problem=1) | :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: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | 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: | | | :white_check_mark: | | | | | | -| 05 | [Smallest multiple](https://projecteuler.net/problem=5) | :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: | | | | | :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: | | | | | | | | |