freeCodeCamp/guide/chinese/certifications/coding-interview-prep/project-euler/problem-5-smallest-multiple/index.md

45 lines
1.1 KiB
Markdown
Raw Normal View History

---
title: Smallest multiple
localeTitle: 最小的倍数
---
## 问题5最小的倍数
### 方法:
* 在这个挑战中我们需要找到1到n个数的LCM。
* 要查找数字的LCM我们使用以下公式
* ![LCM](https://wikimedia.org/api/rest_v1/media/math/render/svg/9453a93953efe119b7502c1827aeeb869ab121d6)
* 为了找到两个数的GCD最大公约数我们使用欧几里德算法。
* 一旦我们得到两个数字的LCM我们就可以得到从1到n的数字的LCM。
### 解:
```js
//LCM of two numbers
function lcm(a, b){
return (a*b)/gcd(a, b);
}
//Euclidean recursive algorithm
function gcd(a, b){
if (b === 0) return a;
return gcd(b, a%b);
}
function smallestMult(n){
let maxLCM = 1;
//Getting the LCM in the range
for (let i = 2; i <= n; i++){
maxLCM = lcm(maxLCM, i);
}
return maxLCM;
}
```
* [运行代码](https://repl.it/@ezioda004/Problem-5-Smallest-multiple)
### 参考文献:
* [欧几里德算法](https://en.wikipedia.org/wiki/Euclidean_algorithm)
* [LCM](https://en.wikipedia.org/wiki/Least_common_multiple)