Skip to content

Problem 5 done in JavaScript #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions JS/05.Smallest_Multiple/05.Smallest_Multiple.js
Original file line number Diff line number Diff line change
@@ -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));
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: | | | | | | | | |
Expand Down