-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix: Adding documentations, tests, and amending algorithm for gcd_of_n_numbers.cpp #2766
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa7bb7a
Update gcd_of_n_numbers.cpp
hollowcrust 9f5586c
Merge branch 'TheAlgorithms:master' into patch-1
hollowcrust 2db4d74
Update gcd_of_n_numbers.cpp
hollowcrust a5582f8
Update gcd_of_n_numbers.cpp
hollowcrust 402ed85
Update gcd_of_n_numbers.cpp
hollowcrust b157b6f
Update gcd_of_n_numbers.cpp
hollowcrust 92b2940
Update gcd_of_n_numbers.cpp
hollowcrust dbae787
Merge branch 'master' into patch-1
hollowcrust 848bb73
Merge branch 'master' into patch-1
hollowcrust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,112 @@ | ||
/** | ||
* @file | ||
* @brief This program aims at calculating the GCD of n numbers by division | ||
* method | ||
* @brief This program aims at calculating the GCD of n numbers | ||
* | ||
* @details | ||
* The GCD of n numbers can be calculated by | ||
* repeatedly calculating the GCDs of pairs of numbers | ||
* i.e. gcd(a, b, c) = gcd(gcd(a, b), c) | ||
* Euclidean algorithm helps calculate the GCD of each pair of numbers efficiently | ||
* | ||
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp | ||
*/ | ||
#include <iostream> | ||
#include <iostream> /// for IO operations | ||
#include <cassert> /// for assert | ||
hollowcrust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
#include <algorithm> /// for std::abs | ||
|
||
/** Compute GCD using division algorithm | ||
* | ||
* @param[in] a array of integers to compute GCD for | ||
* @param[in] n number of integers in array `a` | ||
/** | ||
* @namespace math | ||
* @brief Maths algorithms | ||
*/ | ||
namespace math { | ||
/** | ||
* @namespace gcd_of_n_numbers | ||
* @brief Compute GCD of numbers in an array | ||
*/ | ||
namespace gcd_of_n_numbers { | ||
/** | ||
* @brief Function to compute GCD of 2 numbers x and y | ||
* @param x First number | ||
* @param y Second number | ||
* @return GCD of x and y via recursion | ||
*/ | ||
int gcd_two(int x, int y) { | ||
// base cases | ||
if (y == 0) return x; | ||
if (x == 0) return y; | ||
return gcd_two(y, x % y); // Euclidean method | ||
} | ||
/** | ||
* @brief Function to check if all elements in array are 0 | ||
* @param a Array of numbers | ||
* @param n Number of elements in array | ||
* @return 'True' if all elements are 0 | ||
* @return 'False' if not all elements are 0 | ||
*/ | ||
bool check_all_zeros(int a[], size_t n) { | ||
// Check for the undefined GCD cases | ||
int zero_count = 0; | ||
for (int i = 0; i < n; ++i) { | ||
if (a[i] == 0) { | ||
++zero_count; | ||
} | ||
} | ||
|
||
// return whether all elements in array are 0 | ||
return zero_count == n; | ||
} | ||
/** | ||
* @brief Main program to compute GCD using division algorithm | ||
* @param a Array of integers to compute GCD for | ||
* @param n number of integers in the array | ||
* @return GCD of the numbers in the array | ||
*/ | ||
int gcd(int *a, int n) { | ||
int j = 1; // to access all elements of the array starting from 1 | ||
int gcd(int a[], size_t n) { | ||
hollowcrust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// GCD is undefined if all elements in the array are 0 | ||
if (check_all_zeros(a, n)) | ||
return -1; // since gcd is positive, use -1 to mark undefined gcd | ||
|
||
int gcd = a[0]; | ||
while (j < n) { | ||
if (a[j] % gcd == 0) // value of gcd is as needed so far | ||
j++; // so we check for next element | ||
else | ||
gcd = a[j] % gcd; // calculating GCD by division method | ||
for(int i = 1; i < n; i++) { | ||
gcd = gcd_two(gcd, a[i]); | ||
if (std::abs(gcd) == 1) | ||
break; // if gcd is already 1, further computations still result in gcd of 1 | ||
} | ||
return gcd; | ||
|
||
return std::abs(gcd); // divisors can be negative, and we only want positive value | ||
} | ||
} // namespace gcd_of_n_numbers | ||
} // namespace math | ||
|
||
/** Main function */ | ||
int main() { | ||
int n; | ||
std::cout << "Enter value of n:" << std::endl; | ||
std::cin >> n; | ||
int *a = new int[n]; | ||
int i; | ||
std::cout << "Enter the n numbers:" << std::endl; | ||
for (i = 0; i < n; i++) std::cin >> a[i]; | ||
/** | ||
* @brief Self-test implementation | ||
* @return void | ||
*/ | ||
static void test() { | ||
int array_1[1] = {0}; | ||
int array_2[1] = {1}; | ||
int array_3[2] = {0, 2}; | ||
int array_4[3] = {-60, 24, 18}; | ||
int array_5[4] = {100, -100, -100, 200}; | ||
int array_6[5] = {0, 0, 0, 0, 0}; | ||
hollowcrust marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
int array_7[7] = {90, -120, 0, 135, 660, -280, 900}; | ||
int array_8[7] = {90, -120, 0, 4000, 0, 0, 111}; | ||
|
||
std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl; | ||
assert(math::gcd_of_n_numbers::gcd(array_1, 1) == -1); | ||
assert(math::gcd_of_n_numbers::gcd(array_2, 1) == 1); | ||
assert(math::gcd_of_n_numbers::gcd(array_3, 2) == 2); | ||
assert(math::gcd_of_n_numbers::gcd(array_4, 3) == 6); | ||
assert(math::gcd_of_n_numbers::gcd(array_5, 4) == 100); | ||
assert(math::gcd_of_n_numbers::gcd(array_6, 5) == -1); | ||
assert(math::gcd_of_n_numbers::gcd(array_7, 7) == 5); | ||
assert(math::gcd_of_n_numbers::gcd(array_8, 7) == 1); | ||
} | ||
|
||
delete[] a; | ||
/** | ||
* @brief Main function | ||
* @return 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementation | ||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.