Skip to content

Commit 10373dd

Browse files
add GetGCD method
1 parent 0853f44 commit 10373dd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Maths/GetGCD.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Problem statement and Explanation : https://en.wikipedia.org/wiki/Greatest_common_divisor
3+
4+
In this method, we have followed the iterative approach to first
5+
find a minimum of both numbers and go to the next step.
6+
*/
7+
8+
/**
9+
* GetGCD return the gcd of two numbers.
10+
* @param {Number} arg1 first argument for gcd
11+
* @param {Number} arg2 second argument for gcd
12+
* @returns return a `gcd` value of both number.
13+
*/
14+
const getGcd = (arg1, arg2) => {
15+
// Find a minimum of both numbers.
16+
17+
let less = arg1 > arg2 ? arg2 : arg1
18+
// Iterate the number and find the gcd of the number using the above explanation.
19+
for (less; less >= 2; less--) {
20+
if ((arg1 % less === 0) && (arg2 % less === 0)) return (less)
21+
}
22+
23+
return (less)
24+
}
25+
26+
module.exports = getGcd

0 commit comments

Comments
 (0)