Home  >  Article  >  Web Front-end  >  Find the Greatest Common Divisor

Find the Greatest Common Divisor

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-22 06:29:02569browse

Find the Greatest Common Divisor

Write a function that takes two numbers and returns their greatest common divisor (GCD).

Solution

function findGCD(number1, number2) {
  if(number2 === 0) {
    return number1;
  }

  return findGCD(number2, number1 % number2);
}

console.log(findGCD(-1, -5));
console.log(findGCD(19, 5));
console.log(findGCD(72, 81));
console.log(findGCD(14, 0));

Result

> -1
> 1
> 9
> 14

The above is the detailed content of Find the Greatest Common Divisor. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn