Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 815 Bytes

find_the_greatest_common_divisor_gcd_.md

File metadata and controls

28 lines (19 loc) · 815 Bytes

Find the Greatest Common Divisor (GCD)

To find the GCD of two numbers, you can use the Euclidean algorithm.

Algorithm:

  1. If b is 0, return a as the GCD.
  2. Otherwise, call the function recursively with b and a % b as the arguments.
  3. Repeat until the second number becomes 0.

Example:

function gcd(a, b) {
    if (b === 0) return a;
    return gcd(b, a % b);
}

// Example usage
console.log(gcd(56, 98)); // Output: 14
console.log(gcd(100, 25)); // Output: 25

This method has a time complexity of O(log(min(a, b))), where a and b are the two numbers.

Tags: intermediate, JavaScript, Math, Algorithm