Update gcd.md
This commit is contained in:
@ -2,15 +2,15 @@
|
|||||||
|
|
||||||
Calculates the greatest common divisor between two or more numbers/arrays.
|
Calculates the greatest common divisor between two or more numbers/arrays.
|
||||||
|
|
||||||
The helperGcd function uses recursion.
|
The `helperGcd `function uses recursion.
|
||||||
Base case is when `y` equals `0`. In this case, return `x`.
|
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`.
|
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const gcm = (...arr) => {
|
const gcd = (...arr) => {
|
||||||
let data = [].concat(...arr)
|
let data = [].concat(...arr);
|
||||||
const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
|
const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||||
return data.reduce((a, b) => helperGcd(a, b))
|
return data.reduce((a, b) => helperGcd(a, b));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user