mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
add GetGCD method
This commit is contained in:
26
Maths/GetGCD.js
Normal file
26
Maths/GetGCD.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
Problem statement and Explanation : https://en.wikipedia.org/wiki/Greatest_common_divisor
|
||||||
|
|
||||||
|
In this method, we have followed the iterative approach to first
|
||||||
|
find a minimum of both numbers and go to the next step.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GetGCD return the gcd of two numbers.
|
||||||
|
* @param {Number} arg1 first argument for gcd
|
||||||
|
* @param {Number} arg2 second argument for gcd
|
||||||
|
* @returns return a `gcd` value of both number.
|
||||||
|
*/
|
||||||
|
const getGcd = (arg1, arg2) => {
|
||||||
|
// Find a minimum of both numbers.
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = getGcd
|
Reference in New Issue
Block a user