19 lines
437 B
Markdown
19 lines
437 B
Markdown
### gcd
|
|
|
|
Calculates the greatest common divisor between two or more numbers/arrays.
|
|
|
|
The inner `_gcd` function uses recursion.
|
|
Base case is when `y` equals `0`. In this case, return `x`.
|
|
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
|
|
|
```js
|
|
const gcd = (...arr) => {
|
|
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
|
return [].concat(...arr).reduce((a, b) => _gcd(a, b));
|
|
};
|
|
```
|
|
|
|
```js
|
|
gcd(8, 36); // 4
|
|
```
|