mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
merge: Fix GetEuclidGCD (#1068)
* 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>
This commit is contained in:
@ -18,6 +18,7 @@
|
|||||||
* **Cellular-Automata**
|
* **Cellular-Automata**
|
||||||
* [ConwaysGameOfLife](Cellular-Automata/ConwaysGameOfLife.js)
|
* [ConwaysGameOfLife](Cellular-Automata/ConwaysGameOfLife.js)
|
||||||
* **Ciphers**
|
* **Ciphers**
|
||||||
|
* [AffineCipher](Ciphers/AffineCipher.js)
|
||||||
* [Atbash](Ciphers/Atbash.js)
|
* [Atbash](Ciphers/Atbash.js)
|
||||||
* [CaesarsCipher](Ciphers/CaesarsCipher.js)
|
* [CaesarsCipher](Ciphers/CaesarsCipher.js)
|
||||||
* [KeyFinder](Ciphers/KeyFinder.js)
|
* [KeyFinder](Ciphers/KeyFinder.js)
|
||||||
|
@ -1,32 +1,20 @@
|
|||||||
/*
|
|
||||||
Problem statement and Explanation : https://en.wikipedia.org/wiki/Euclidean_algorithm
|
|
||||||
|
|
||||||
In this method, we have followed the iterative approach to first
|
|
||||||
find a minimum of both numbers and go to the next step.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetEuclidGCD return the gcd of two numbers using Euclidean algorithm.
|
* GetEuclidGCD Euclidean algorithm to determine the GCD of two numbers
|
||||||
* @param {Number} arg1 first argument for gcd
|
* @param {Number} a integer (may be negative)
|
||||||
* @param {Number} arg2 second argument for gcd
|
* @param {Number} b integer (may be negative)
|
||||||
* @returns return a `gcd` value of both number.
|
* @returns {Number} Greatest Common Divisor gcd(a, b)
|
||||||
*/
|
*/
|
||||||
const GetEuclidGCD = (arg1, arg2) => {
|
export function GetEuclidGCD (a, b) {
|
||||||
// firstly, check that input is a number or not.
|
if (typeof a !== 'number' || typeof b !== 'number') {
|
||||||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
throw new TypeError('Arguments must be numbers')
|
||||||
return new TypeError('Argument is not a number.')
|
|
||||||
}
|
}
|
||||||
// check that the input number is not a negative value.
|
if (a === 0 && b === 0) return undefined // infinitely many numbers divide 0
|
||||||
if (arg1 < 1 || arg2 < 1) {
|
a = Math.abs(a)
|
||||||
return new TypeError('Argument is a negative number.')
|
b = Math.abs(b)
|
||||||
|
while (b !== 0) {
|
||||||
|
const rem = a % b
|
||||||
|
a = b
|
||||||
|
b = rem
|
||||||
}
|
}
|
||||||
// Find a minimum of both numbers.
|
return a
|
||||||
let less = arg1 > arg2 ? arg2 : arg1
|
|
||||||
// Iterate the number and find the gcd of the number using the above explanation.
|
|
||||||
for (less; less >= 2; less--) {
|
|
||||||
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
|
|
||||||
}
|
|
||||||
return (less)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { GetEuclidGCD }
|
|
||||||
|
Reference in New Issue
Block a user