Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,25 @@
---
title: Least common multiple
type: snippet
tags: [math,algorithm,recursion]
cover: waving-over-lake
dateModified: 2020-12-28T13:49:24+02:00
---
Calculates the least common multiple of two or more numbers.
- Use the greatest common divisor (GCD) formula and the fact that `lcm(x, y) = x * y / gcd(x, y)` to determine the least common multiple.
- The GCD formula uses recursion.
```js
const lcm = (...arr) => {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const _lcm = (x, y) => (x * y) / gcd(x, y);
return [...arr].reduce((a, b) => _lcm(a, b));
};
```
```js
lcm(12, 7); // 84
lcm(...[1, 3, 4, 5]); // 60
```