|
1 | | -function euclideanGCDRecursive (first, second) { |
2 | | - /* |
3 | | - Calculates GCD of two numbers using Euclidean Recursive Algorithm |
4 | | - :param first: First number |
5 | | - :param second: Second number |
6 | | - :return: GCD of the numbers |
7 | | - */ |
8 | | - if (second == 0) { |
9 | | - return first; |
10 | | - } else { |
11 | | - return euclideanGCDRecursive(second, (first % second)); |
12 | | - } |
| 1 | +function euclideanGCDRecursive(first, second) { |
| 2 | + /* |
| 3 | + Calculates GCD of two numbers using Euclidean Recursive Algorithm |
| 4 | + :param first: First number |
| 5 | + :param second: Second number |
| 6 | + :return: GCD of the numbers |
| 7 | + */ |
| 8 | + if (second === 0) { |
| 9 | + return first; |
| 10 | + } else { |
| 11 | + return euclideanGCDRecursive(second, (first % second)); |
| 12 | + } |
13 | 13 | } |
14 | 14 |
|
15 | | -function euclideanGCDIterative (first, second) { |
16 | | - /* |
17 | | - Calculates GCD of two numbers using Euclidean Iterative Algorithm |
18 | | - :param first: First number |
19 | | - :param second: Second number |
20 | | - :return: GCD of the numbers |
21 | | - */ |
22 | | - while (second != 0) { |
23 | | - let temp = second; |
24 | | - second = first % second; |
25 | | - first = temp; |
26 | | - } |
27 | | - return first; |
| 15 | +function euclideanGCDIterative(first, second) { |
| 16 | + /* |
| 17 | + Calculates GCD of two numbers using Euclidean Iterative Algorithm |
| 18 | + :param first: First number |
| 19 | + :param second: Second number |
| 20 | + :return: GCD of the numbers |
| 21 | + */ |
| 22 | + while (second !== 0) { |
| 23 | + let temp = second; |
| 24 | + second = first % second; |
| 25 | + first = temp; |
| 26 | + } |
| 27 | + return first; |
28 | 28 | } |
29 | 29 |
|
30 | | -function main () { |
31 | | - let first = 20; |
32 | | - let second = 30; |
33 | | - console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second)); |
34 | | - console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second)); |
| 30 | +function main() { |
| 31 | + let first = 20; |
| 32 | + let second = 30; |
| 33 | + console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second)); |
| 34 | + console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second)); |
35 | 35 | } |
36 | 36 |
|
37 | 37 | main(); |
0 commit comments