From e31ee6e83332e9c6e508e8bd88f216ce6042d40f Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 15:42:12 -0400 Subject: [PATCH] better document GCD programs --- ...lidean.cpp => gcd_iterative_euclidean.cpp} | 18 +++++-- math/gcd_recursive_euclidean.cpp | 48 +++++++++++++++++++ math/greatest_common_divisor.cpp | 27 ----------- 3 files changed, 62 insertions(+), 31 deletions(-) rename math/{greatest_common_divisor_euclidean.cpp => gcd_iterative_euclidean.cpp} (80%) create mode 100644 math/gcd_recursive_euclidean.cpp delete mode 100644 math/greatest_common_divisor.cpp diff --git a/math/greatest_common_divisor_euclidean.cpp b/math/gcd_iterative_euclidean.cpp similarity index 80% rename from math/greatest_common_divisor_euclidean.cpp rename to math/gcd_iterative_euclidean.cpp index c4812e45b..61fb640a3 100644 --- a/math/greatest_common_divisor_euclidean.cpp +++ b/math/gcd_iterative_euclidean.cpp @@ -1,10 +1,17 @@ -#include +/** + * @file + * @brief Compute the greatest common denominator of two integers using + * *iterative form* of + * [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) + * + * @see gcd_recursive_euclidean.cpp + */ #include #include -// will find the greatest common denominator of two ints integers -// Euclidean algorithm can be found here -// https://en.wikipedia.org/wiki/Euclidean_algorithm +/** + * algorithm + */ int gcd(int num1, int num2) { if (num1 <= 0 | num2 <= 0) { throw std::domain_error("Euclidean algorithm domain is for ints > 0"); @@ -34,6 +41,9 @@ int gcd(int num1, int num2) { return previous_remainder; } +/** + * Main function + */ int main() { std::cout << "gcd of 120,7 is " << (gcd(120, 7)) << std::endl; try { diff --git a/math/gcd_recursive_euclidean.cpp b/math/gcd_recursive_euclidean.cpp new file mode 100644 index 000000000..3a1ca6e66 --- /dev/null +++ b/math/gcd_recursive_euclidean.cpp @@ -0,0 +1,48 @@ +/** + * @file + * @brief Compute the greatest common denominator of two integers using + * *recursive form* of + * [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm) + * + * @see gcd_iterative_euclidean.cpp + */ +#include + +/** + * algorithm + */ +int gcd(int num1, int num2) { + if (num1 <= 0 | num2 <= 0) { + throw std::domain_error("Euclidean algorithm domain is for ints > 0"); + } + + if (num1 == num2) { + return num1; + } + + // Everything divides 0 + if (num1 == 0) return num2; + if (num2 == 0) return num1; + + // base case + if (num1 == num2) return num1; + + // a is greater + if (num1 > num2) return gcd(num1 - num2, num2); + return gcd(num1, num2 - num1); +} + +/** + * Main function + */ +int main() { + std::cout << "gcd of 120,7 is " << (gcd(120, 7)) << std::endl; + try { + std::cout << "gcd of -120,10 is " << gcd(-120, 10) << std::endl; + } catch (const std::domain_error &e) { + std::cout << "Error handling was successful" << std::endl; + } + std::cout << "gcd of 312,221 is " << (gcd(312, 221)) << std::endl; + std::cout << "gcd of 289,204 is " << (gcd(289, 204)) << std::endl; + return 0; +} diff --git a/math/greatest_common_divisor.cpp b/math/greatest_common_divisor.cpp deleted file mode 100644 index 5601c4be9..000000000 --- a/math/greatest_common_divisor.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// C++ program to find GCD of two numbers -#include - -// Recursive function to return gcd of a and b -int gcd(int a, int b) { - // Everything divides 0 - if (a == 0) - return b; - if (b == 0) - return a; - - // base case - if (a == b) - return a; - - // a is greater - if (a > b) - return gcd(a-b, b); - return gcd(a, b-a); -} - -// Driver program to test above function -int main() { - int a = 98, b = 56; - std::cout << "GCD of " << a << " and " << b << " is " << gcd(a, b); - return 0; -}