Problem 14

Greatest Common Divisor Recursion Challenge

Given two positive integers, return their greatest common divisor (GCD). The GCD is the largest number that divides both inputs without a remainder. If either input is negative, return null.

Function Signature

gcd(x, y)

Parameters

  • x — an integer.
  • y — an integer.

Output

Return a number representing the GCD of x and y, or null if either value is negative.

Constraints

  • The function must use recursion.
  • The function should accept exactly two arguments.

Examples

gcd(4, 36)    → 4
gcd(24, 88)   → 8
gcd(339, 17)  → 1
gcd(126, 900) → 18
gcd(-4, 2)    → null
gcd(-10, -58) → null