forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEucledianGCD.js
More file actions
30 lines (28 loc) · 742 Bytes
/
EucledianGCD.js
File metadata and controls
30 lines (28 loc) · 742 Bytes
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
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) {
const temp = second
second = first % second
first = temp
}
return first
}
export { euclideanGCDIterative, euclideanGCDRecursive }