forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 5
/
euclideanGCD.js
37 lines (34 loc) · 917 Bytes
/
euclideanGCD.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function euclideanGCDRecursive (first, second) {
/*
Calculates GCD of two numbers using Euclidean Recursive Algorithm
:param first: First number
:param second: Second number
:return: GCD of the numbers
*/
if (second === 0) {
return first;
} else {
return euclideanGCDRecursive(second, (first % second));
}
}
function euclideanGCDIterative (first, second) {
/*
Calculates GCD of two numbers using Euclidean Iterative Algorithm
:param first: First number
:param second: Second number
:return: GCD of the numbers
*/
while (second !== 0) {
let temp = second;
second = first % second;
first = temp;
}
return first;
}
function main () {
let first = 20;
let second = 30;
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second));
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second));
}
main();