File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments