Files
30-seconds-of-code/snippets/gcd.md
2017-12-17 17:55:51 +02:00

13 lines
296 B
Markdown

### gcd
Calculates the greatest common divisor between two numbers.
Use 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 = (x, y) => !y ? x : gcd(y, x % y);
// gcd (8, 36) -> 4
```