Files
30-seconds-of-code/snippets/greatest-common-divisor.md
Stefan Feješ f559030eac fix naming
2017-12-17 15:41:31 +01:00

279 B

Greatest common divisor (GCD)

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.

const greatestCommonDivisor = (x, y) => !y ? x : gcd(y, x % y);
// gcd (8, 36) -> 4