Home > Web Front-end > JS Tutorial > Find the Greatest Common Divisor

Find the Greatest Common Divisor

Mary-Kate Olsen
Release: 2024-10-22 06:29:02
Original
651 people have browsed it

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));
Copy after login

Result

> -1
> 1
> 9
> 14
Copy after login

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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template