dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
26 lines (21 loc) • 812 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = euclideanAlgorithmIterative;
/**
* Iterative version of Euclidean Algorithm of finding greatest common divisor (GCD).
* @param {number} originalA
* @param {number} originalB
* @return {number}
*/
function euclideanAlgorithmIterative(originalA, originalB) {
// Make input numbers positive.
let a = Math.abs(originalA);
let b = Math.abs(originalB); // Subtract one number from another until both numbers would become the same.
// This will be out GCD. Also quit the loop if one of the numbers is zero.
while (a && b && a !== b) {
[a, b] = a > b ? [a - b, b] : [a, b - a];
} // Return the number that is not equal to zero since the last subtraction (it will be a GCD).
return a || b;
}