SIGN IN SIGN UP

Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.

0 0 0 JavaScript
2020-05-03 09:05:12 +02:00
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
*/
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) {
/*
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
}
export { euclideanGCDIterative, euclideanGCDRecursive }