mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
21 lines
698 B
JavaScript
21 lines
698 B
JavaScript
/**
|
|
* Iterative version of Euclidean Algorithm of finding greatest common divisor (GCD).
|
|
* @param {number} originalA
|
|
* @param {number} originalB
|
|
* @return {number}
|
|
*/
|
|
export default 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;
|
|
}
|