mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00

* Fix GetEuclidGCD Implement the actual Euclidean Algorithm * Replace == with === * Lua > JS * Standard sucks * Oops * Update GetEuclidGCD.js * Updated Documentation in README.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
21 lines
576 B
JavaScript
21 lines
576 B
JavaScript
/**
|
|
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers
|
|
* @param {Number} a integer (may be negative)
|
|
* @param {Number} b integer (may be negative)
|
|
* @returns {Number} Greatest Common Divisor gcd(a, b)
|
|
*/
|
|
export function GetEuclidGCD (a, b) {
|
|
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
throw new TypeError('Arguments must be numbers')
|
|
}
|
|
if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0
|
|
a = Math.abs(a)
|
|
b = Math.abs(b)
|
|
while (b !== 0) {
|
|
const rem = a % b
|
|
a = b
|
|
b = rem
|
|
}
|
|
return a
|
|
}
|