mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 22:46:39 +08:00
include see-also for all gcd algos
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* *iterative form* of
|
||||
* [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm)
|
||||
*
|
||||
* @see gcd_recursive_euclidean.cpp
|
||||
* @see gcd_recursive_euclidean.cpp, gcd_of_n_numbers.cpp
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
* @brief This program aims at calculating the GCD of n numbers by division
|
||||
* method
|
||||
*
|
||||
* @see ../math/greatest_common_divisor.cpp
|
||||
* greatest_common_divisor_euclidean.cpp
|
||||
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* *recursive form* of
|
||||
* [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm)
|
||||
*
|
||||
* @see gcd_iterative_euclidean.cpp
|
||||
* @see gcd_iterative_euclidean.cpp, gcd_of_n_numbers.cpp
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
@@ -21,14 +21,18 @@ int gcd(int num1, int num2) {
|
||||
}
|
||||
|
||||
// Everything divides 0
|
||||
if (num1 == 0) return num2;
|
||||
if (num2 == 0) return num1;
|
||||
if (num1 == 0)
|
||||
return num2;
|
||||
if (num2 == 0)
|
||||
return num1;
|
||||
|
||||
// base case
|
||||
if (num1 == num2) return num1;
|
||||
if (num1 == num2)
|
||||
return num1;
|
||||
|
||||
// a is greater
|
||||
if (num1 > num2) return gcd(num1 - num2, num2);
|
||||
if (num1 > num2)
|
||||
return gcd(num1 - num2, num2);
|
||||
return gcd(num1, num2 - num1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user