How to resolve the algorithm Greatest common divisor step by step in the JavaScript programming language
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 integersa
andb
using the Euclidean algorithm. It converts both integers to absolute values to handle negative numbers. Then, it checks ifb
is greater thana
and swaps them if necessary. It enters a loop that continues until eithera
orb
becomes zero. Inside the loop, it calculates the remainder ofa
divided byb
, assigns it toa
, and checks ifa
is zero. Ifa
is zero, it returnsb
as the GCD. Ifa
is not zero, it calculates the remainder ofb
divided bya
, assigns it tob
, and checks ifb
is zero. Ifb
is zero, it returnsa
as the GCD. -
gcd_rec(a, b)
: This function calculates the GCD of two integersa
andb
using a recursive approach. It checks ifb
is zero. Ifb
is zero, it returns the absolute value ofa
as the GCD. Otherwise, it makes a recursive call togcd_rec
withb
and the remainder ofa
divided byb
as arguments. -
GCD(arr)
: This function calculates the GCD of an array of integersarr
. It initializes variablesi
,y
,n
, andx
. The variablen
is the length of thearr
array. It initializesx
to the absolute value of the first element of thearr
array. Then, it iterates through the remaining elements of thearr
array, calculating the GCD of each element with the currentx
value. Inside the loop, it calculates the absolute value of the current element of thearr
array and assigns it toy
. It then enters another loop that continues as long asx
andy
are both non-zero. Inside this inner loop, it checks ifx
is greater thany
and calculates the remainder ofx
divided byy
if it is. Otherwise, it calculates the remainder ofy
divided byx
. After this inner loop, it addsx
andy
and assigns the result tox
. Finally, it returnsx
as the GCD of thearr
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