2020-05-03 09:05:12 +02:00
|
|
|
function euclideanGCDRecursive (first, second) {
|
|
|
|
|
/*
|
2019-06-27 10:41:44 -04:00
|
|
|
Calculates GCD of two numbers using Euclidean Recursive Algorithm
|
|
|
|
|
:param first: First number
|
|
|
|
|
:param second: Second number
|
|
|
|
|
:return: GCD of the numbers
|
|
|
|
|
*/
|
2020-05-03 09:05:12 +02:00
|
|
|
if (second === 0) {
|
|
|
|
|
return first
|
|
|
|
|
} else {
|
|
|
|
|
return euclideanGCDRecursive(second, (first % second))
|
|
|
|
|
}
|
2017-09-16 21:14:16 +05:30
|
|
|
}
|
|
|
|
|
|
2020-05-03 09:05:12 +02:00
|
|
|
function euclideanGCDIterative (first, second) {
|
|
|
|
|
/*
|
2019-06-27 10:41:44 -04:00
|
|
|
Calculates GCD of two numbers using Euclidean Iterative Algorithm
|
|
|
|
|
:param first: First number
|
|
|
|
|
:param second: Second number
|
|
|
|
|
:return: GCD of the numbers
|
|
|
|
|
*/
|
2020-05-03 09:05:12 +02:00
|
|
|
while (second !== 0) {
|
|
|
|
|
const temp = second
|
|
|
|
|
second = first % second
|
|
|
|
|
first = temp
|
|
|
|
|
}
|
|
|
|
|
return first
|
2017-09-16 21:14:16 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-10 18:18:25 +02:00
|
|
|
export { euclideanGCDIterative, euclideanGCDRecursive }
|