mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
change the GetGCD method to GetEuclidGCD method
This commit is contained in:
@ -6,24 +6,27 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GetGCD return the gcd of two numbers.
|
* GetEuclidGCD return the gcd of two numbers using Euclidean algorithm.
|
||||||
* @param {Number} arg1 first argument for gcd
|
* @param {Number} arg1 first argument for gcd
|
||||||
* @param {Number} arg2 second argument for gcd
|
* @param {Number} arg2 second argument for gcd
|
||||||
* @returns return a `gcd` value of both number.
|
* @returns return a `gcd` value of both number.
|
||||||
*/
|
*/
|
||||||
const getGcd = (arg1, arg2) => {
|
const GetEuclidGCD = (arg1, arg2) => {
|
||||||
// firstly, check that input is a number or not.
|
// firstly, check that input is a number or not.
|
||||||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') {
|
||||||
return new TypeError('Argument is not a number.')
|
return new TypeError('Argument is not a number.')
|
||||||
}
|
}
|
||||||
|
// check that the input number is not a negative value.
|
||||||
|
if (arg1 < 1 || arg2 < 1) {
|
||||||
|
return new TypeError('Argument is a negative number.')
|
||||||
|
}
|
||||||
// Find a minimum of both numbers.
|
// Find a minimum of both numbers.
|
||||||
let less = arg1 > arg2 ? arg2 : arg1
|
let less = arg1 > arg2 ? arg2 : arg1
|
||||||
// Iterate the number and find the gcd of the number using the above explanation.
|
// Iterate the number and find the gcd of the number using the above explanation.
|
||||||
for (less; less >= 2; less--) {
|
for (less; less >= 2; less--) {
|
||||||
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
|
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (less)
|
return (less)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = getGcd
|
module.exports = GetEuclidGCD
|
||||||
|
Reference in New Issue
Block a user