mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Re-orgainze files and folders in the repository (#172)
* Re-orgainze files and folders in the repository * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
37
Recursive/EucledianGCD.js
Normal file
37
Recursive/EucledianGCD.js
Normal file
@ -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) {
|
||||
const temp = second
|
||||
second = first % second
|
||||
first = temp
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
function main () {
|
||||
const first = 20
|
||||
const 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()
|
Reference in New Issue
Block a user