2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
id: 5900f3711000cf542c50fe84
|
|
|
|
title: 'Problem 5: Smallest multiple'
|
2020-11-27 18:02:05 +00:00
|
|
|
challengeType: 5
|
2019-08-05 16:17:33 +00:00
|
|
|
forumTopicId: 302160
|
2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --description--
|
2020-02-28 12:39:47 +00:00
|
|
|
|
2018-09-30 22:01:58 +00:00
|
|
|
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
2020-02-28 12:39:47 +00:00
|
|
|
|
|
|
|
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to `n`?
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --hints--
|
|
|
|
|
|
|
|
`smallestMult(5)` should return a number.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert(typeof smallestMult(5) === 'number');
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`smallestMult(5)` should return 60.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.strictEqual(smallestMult(5), 60);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`smallestMult(7)` should return 420.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.strictEqual(smallestMult(7), 420);
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`smallestMult(10)` should return 2520.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.strictEqual(smallestMult(10), 2520);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`smallestMult(13)` should return 360360.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```js
|
2020-11-27 18:02:05 +00:00
|
|
|
assert.strictEqual(smallestMult(13), 360360);
|
|
|
|
```
|
2020-09-15 16:57:40 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
`smallestMult(20)` should return 232792560.
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
assert.strictEqual(smallestMult(20), 232792560);
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --seed--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
## --seed-contents--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
```js
|
|
|
|
function smallestMult(n) {
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
smallestMult(20);
|
|
|
|
```
|
2018-09-30 22:01:58 +00:00
|
|
|
|
2020-11-27 18:02:05 +00:00
|
|
|
# --solutions--
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```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;
|
|
|
|
}
|
|
|
|
```
|