How to calculate GCD of two or more numbers/arrays in JavaScript?


The greatest common divisor (GCD) of two or more numbers, also known as the greatest common factor (GCF) or highest common factor (HCF), is the largest positive integer that divides a given number without a remainder. In other words, the GCD is the largest number that is a divisor of both numbers.

For example, the GCD of 24 and 36 is 12.

How to calculate the GCD of two numbers?

There are a few different ways to calculate the GCD of two numbers, but the most common method is the Euclidean algorithm.

The Euclidean algorithm is an iterative method that starts with two numbers, a and b, and finds the GCD of a and b. The basic idea behind the Euclidean algorithm is to keep subtracting the smaller number from the larger number until the two numbers are equal.

  • For example, let's find the GCD of 24 and 36 using the Euclidean algorithm.

  • Starting with 24 and 36, we subtract the smaller number (24) from the larger number (36), which leaves us with 12.

  • Then, we subtract the smaller number (12) from the larger number (24), which leaves us with 12.

  • Since the two numbers are now equal, we have found the GCD! In this case, the GCD is 12.

How to calculate the GCD of more than two numbers?

The Euclidean algorithm can also be used to calculate the GCD of more than two numbers. The basic idea is the same as before, but instead of subtracting the smaller number from the larger number, you subtract the GCD of the two numbers from the larger number.

  • For example, let's find the GCD of 24, 36, and 48.

  • First, we use the Euclidean algorithm to find the GCD of 24 and 36, which is 12.

  • Then, we use the Euclidean algorithm again to find the GCD of 36 and 48, which is 12.

  • Finally, we use the Euclidean algorithm one last time to find the GCD of 48 and 12, which is 12.

  • Since the GCD of 24, 36, and 48 is 12, we can stop here.

Example

Here is a full working code example of how to calculate the GCD of two or more numbers in JavaScript.

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <h2>Calculating GCD (Greatest Common Divisor)</h2>
   <div id="result1"></div>
   <div id="result2"></div>
   <script>
      function gcd(a, b) {
         // Make sure a is larger than b
         if (a < b) {
            var temp = a;
            a = b;
            b = temp;
         }

         // Iteratively subtract the smaller number from the larger number
         // until the two numbers are equal
         while (b != 0) {
            var temp = b;
            b = a % b;
            a = temp;
         }

         // Return the GCD
         return a;
      }
      // Calculate the GCD of 24 and 36
      var n1 = 24;
      var n2 = 36;
      var result = gcd(n1, n2);
      document.getElementById("result1").innerHTML = `GCD of ${n1} and ${n2} = ` + result;

      // Calculate the GCD of 24, 36, and 48
      var n1 = 8;
      var n2 = 12;
      var n3 = 20;
      var result = gcd(n1, n2, n3);
      document.getElementById("result2").innerHTML = `<br> GCD of ${n1}, ${n2}, and ${n3} =1`+ result;
   </script>
</body>
</html>

Conclusion

In this article, we learned how to calculate the greatest common divisor (GCD) of two or more numbers using the Euclidean algorithm.

Updated on: 01-Jul-2022

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements