2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 5900f3711000cf542c50fe84
|
|
|
|
challengeType: 5
|
|
|
|
title: 'Problem 5: Smallest multiple'
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
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 <code>n</code>?
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 13:37:37 +00:00
|
|
|
tests:
|
|
|
|
- text: <code>smallestMult(5)</code> should return 60.
|
2018-10-08 00:01:53 +00:00
|
|
|
testString: 'assert.strictEqual(smallestMult(5), 60, "<code>smallestMult(5)</code> should return 60.");'
|
2018-10-04 13:37:37 +00:00
|
|
|
- text: <code>smallestMult(7)</code> should return 420.
|
2018-10-08 00:01:53 +00:00
|
|
|
testString: 'assert.strictEqual(smallestMult(7), 420, "<code>smallestMult(7)</code> should return 420.");'
|
2018-10-04 13:37:37 +00:00
|
|
|
- text: <code>smallestMult(10)</code> should return 2520.
|
2018-10-08 00:01:53 +00:00
|
|
|
testString: 'assert.strictEqual(smallestMult(10), 2520, "<code>smallestMult(10)</code> should return 2520.");'
|
2018-10-04 13:37:37 +00:00
|
|
|
- text: <code>smallestMult(13)</code> should return 360360.
|
2018-10-08 00:01:53 +00:00
|
|
|
testString: 'assert.strictEqual(smallestMult(13), 360360, "<code>smallestMult(13)</code> should return 360360.");'
|
2018-10-04 13:37:37 +00:00
|
|
|
- text: <code>smallestMult(20)</code> should return 232792560.
|
2018-10-08 00:01:53 +00:00
|
|
|
testString: 'assert.strictEqual(smallestMult(20), 232792560, "<code>smallestMult(20)</code> should return 232792560.");'
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function smallestMult(n) {
|
|
|
|
// Good luck!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
smallestMult(20);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
function smallestMult(n){
|
|
|
|
function gcd(a, b) {
|
|
|
|
return b === 0 ? a : gcd(b, a%b); // Euclidean algorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
function lcm(a, b) {
|
|
|
|
return a * b / gcd(a, b);
|
|
|
|
}
|
|
|
|
var result = 1;
|
|
|
|
for(var i = 2; i <= n; i++) {
|
|
|
|
result = lcm(result, i);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|