How to resolve the algorithm Greatest common divisor step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest common divisor step by step in the JavaScript programming language

Table of Contents

Problem Statement

Find the greatest common divisor   (GCD)   of two integers.

Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greatest common divisor step by step in the JavaScript programming language

The provided code contains three JavaScript functions:

  • The gcd function calculates the greatest common divisor (GCD) of two integers using the Euclidean algorithm. It handles negative integers by converting them to absolute values.

  • The gcd_rec function calculates the GCD of two integers using a recursive approach. It repeatedly calculates the remainder of the larger number divided by the smaller number until one of them becomes zero.

  • The GCD function calculates the GCD of an array of integers using a loop. It iterates through the integers, calculating the GCD of each integer with the current GCD, and returning the final GCD.

Here's how each function works:

  • gcd(a, b): This function calculates the GCD of two integers a and b using the Euclidean algorithm. It converts both integers to absolute values to handle negative numbers. Then, it checks if b is greater than a and swaps them if necessary. It enters a loop that continues until either a or b becomes zero. Inside the loop, it calculates the remainder of a divided by b, assigns it to a, and checks if a is zero. If a is zero, it returns b as the GCD. If a is not zero, it calculates the remainder of b divided by a, assigns it to b, and checks if b is zero. If b is zero, it returns a as the GCD.

  • gcd_rec(a, b): This function calculates the GCD of two integers a and b using a recursive approach. It checks if b is zero. If b is zero, it returns the absolute value of a as the GCD. Otherwise, it makes a recursive call to gcd_rec with b and the remainder of a divided by b as arguments.

  • GCD(arr): This function calculates the GCD of an array of integers arr. It initializes variables i, y, n, and x. The variable n is the length of the arr array. It initializes x to the absolute value of the first element of the arr array. Then, it iterates through the remaining elements of the arr array, calculating the GCD of each element with the current x value. Inside the loop, it calculates the absolute value of the current element of the arr array and assigns it to y. It then enters another loop that continues as long as x and y are both non-zero. Inside this inner loop, it checks if x is greater than y and calculates the remainder of x divided by y if it is. Otherwise, it calculates the remainder of y divided by x. After this inner loop, it adds x and y and assigns the result to x. Finally, it returns x as the GCD of the arr array.

Source code in the javascript programming language

function gcd(a,b) {
  a = Math.abs(a);
  b = Math.abs(b);

  if (b > a) {
    var temp = a;
    a = b;
    b = temp; 
  }

  while (true) {
    a %= b;
    if (a === 0) { return b; }
    b %= a;
    if (b === 0) { return a; }
  }
}


function gcd_rec(a, b) {
  return b ? gcd_rec(b, a % b) : Math.abs(a);
}


function GCD(arr) {
  var i, y,
      n = arr.length,
      x = Math.abs(arr[0]);

  for (i = 1; i < n; i++) {
    y = Math.abs(arr[i]);

    while (x && y) {
      (x > y) ? x %= y : y %= x;
    }
    x += y;
  }
  return x;
}

//For example:
GCD([57,0,-45,-18,90,447]); //=> 3


  

You may also check:How to resolve the algorithm String interpolation (included) step by step in the Phix programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the zkl programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Factor programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the Lua programming language
You may also check:How to resolve the algorithm Partial function application step by step in the OCaml programming language