Skip to content

Commit cedbe13

Browse files
authored
Merge pull request TheAlgorithms#12 from ms10398/master
Implemented Eucledian GCD
2 parents c4cc20c + b29f953 commit cedbe13

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Algorithms/EucledianGCD.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function euclideanGCDRecursive (first, second) {
2+
/*
3+
Calculates GCD of two numbers using Euclidean Recursive Algorithm
4+
:param first: First number
5+
:param second: Second number
6+
:return: GCD of the numbers
7+
*/
8+
if (second === 0) {
9+
return first;
10+
} else {
11+
return euclideanGCDRecursive(second, (first % second));
12+
}
13+
}
14+
15+
function euclideanGCDIterative (first, second) {
16+
/*
17+
Calculates GCD of two numbers using Euclidean Iterative Algorithm
18+
:param first: First number
19+
:param second: Second number
20+
:return: GCD of the numbers
21+
*/
22+
while (second !== 0) {
23+
var temp = second;
24+
second = first % second;
25+
first = temp;
26+
}
27+
return first;
28+
}
29+
30+
function main () {
31+
var first = 20;
32+
var second = 30;
33+
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
34+
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
35+
}
36+
37+
main();

0 commit comments

Comments
 (0)