Skip to content

Implemented Eucledian GCD #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Algorithms/EucledianGCD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) {
var temp = second;
second = first % second;
first = temp;
}
return first;
}

function main () {
var first = 20;
var 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();