Skip to content

Commit fddedd8

Browse files
authored
fix: Adding documentations, tests, and amending algorithm for gcd_of_n_numbers.cpp (#2766)
* Update gcd_of_n_numbers.cpp * Update gcd_of_n_numbers.cpp Reformatting code, comment and test cases, change array data type. * Update gcd_of_n_numbers.cpp * Update gcd_of_n_numbers.cpp * Update gcd_of_n_numbers.cpp * Update gcd_of_n_numbers.cpp
1 parent b957b1d commit fddedd8

File tree

1 file changed

+102
-29
lines changed

1 file changed

+102
-29
lines changed

math/gcd_of_n_numbers.cpp

Lines changed: 102 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,114 @@
11
/**
22
* @file
3-
* @brief This program aims at calculating the GCD of n numbers by division
4-
* method
3+
* @brief This program aims at calculating the GCD of n numbers
4+
*
5+
* @details
6+
* The GCD of n numbers can be calculated by
7+
* repeatedly calculating the GCDs of pairs of numbers
8+
* i.e. \f$\gcd(a, b, c)\f$ = \f$\gcd(\gcd(a, b), c)\f$
9+
* Euclidean algorithm helps calculate the GCD of each pair of numbers
10+
* efficiently
511
*
612
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
713
*/
8-
#include <iostream>
14+
#include <algorithm> /// for std::abs
15+
#include <array> /// for std::array
16+
#include <cassert> /// for assert
17+
#include <iostream> /// for IO operations
918

10-
/** Compute GCD using division algorithm
11-
*
12-
* @param[in] a array of integers to compute GCD for
13-
* @param[in] n number of integers in array `a`
14-
*/
15-
int gcd(int *a, int n) {
16-
int j = 1; // to access all elements of the array starting from 1
17-
int gcd = a[0];
18-
while (j < n) {
19-
if (a[j] % gcd == 0) // value of gcd is as needed so far
20-
j++; // so we check for next element
21-
else
22-
gcd = a[j] % gcd; // calculating GCD by division method
19+
/**
20+
* @namespace math
21+
* @brief Maths algorithms
22+
*/
23+
namespace math {
24+
/**
25+
* @namespace gcd_of_n_numbers
26+
* @brief Compute GCD of numbers in an array
27+
*/
28+
namespace gcd_of_n_numbers {
29+
/**
30+
* @brief Function to compute GCD of 2 numbers x and y
31+
* @param x First number
32+
* @param y Second number
33+
* @return GCD of x and y via recursion
34+
*/
35+
int gcd_two(int x, int y) {
36+
// base cases
37+
if (y == 0) {
38+
return x;
39+
}
40+
if (x == 0) {
41+
return y;
42+
}
43+
return gcd_two(y, x % y); // Euclidean method
44+
}
45+
46+
/**
47+
* @brief Function to check if all elements in the array are 0
48+
* @param a Array of numbers
49+
* @return 'True' if all elements are 0
50+
* @return 'False' if not all elements are 0
51+
*/
52+
template <std::size_t n>
53+
bool check_all_zeros(const std::array<int, n> &a) {
54+
// Use std::all_of to simplify zero-checking
55+
return std::all_of(a.begin(), a.end(), [](int x) { return x == 0; });
56+
}
57+
58+
/**
59+
* @brief Main program to compute GCD using the Euclidean algorithm
60+
* @param a Array of integers to compute GCD for
61+
* @return GCD of the numbers in the array or std::nullopt if undefined
62+
*/
63+
template <std::size_t n>
64+
int gcd(const std::array<int, n> &a) {
65+
// GCD is undefined if all elements in the array are 0
66+
if (check_all_zeros(a)) {
67+
return -1; // Use std::optional to represent undefined GCD
68+
}
69+
70+
// divisors can be negative, we only want the positive value
71+
int result = std::abs(a[0]);
72+
for (std::size_t i = 1; i < n; ++i) {
73+
result = gcd_two(result, std::abs(a[i]));
74+
if (result == 1) {
75+
break; // Further computations still result in gcd of 1
2376
}
24-
return gcd;
77+
}
78+
return result;
2579
}
80+
} // namespace gcd_of_n_numbers
81+
} // namespace math
2682

27-
/** Main function */
28-
int main() {
29-
int n;
30-
std::cout << "Enter value of n:" << std::endl;
31-
std::cin >> n;
32-
int *a = new int[n];
33-
int i;
34-
std::cout << "Enter the n numbers:" << std::endl;
35-
for (i = 0; i < n; i++) std::cin >> a[i];
83+
/**
84+
* @brief Self-test implementation
85+
* @return void
86+
*/
87+
static void test() {
88+
std::array<int, 1> array_1 = {0};
89+
std::array<int, 1> array_2 = {1};
90+
std::array<int, 2> array_3 = {0, 2};
91+
std::array<int, 3> array_4 = {-60, 24, 18};
92+
std::array<int, 4> array_5 = {100, -100, -100, 200};
93+
std::array<int, 5> array_6 = {0, 0, 0, 0, 0};
94+
std::array<int, 7> array_7 = {10350, -24150, 0, 17250, 37950, -127650, 51750};
95+
std::array<int, 7> array_8 = {9500000, -12121200, 0, 4444, 0, 0, 123456789};
3696

37-
std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl;
97+
assert(math::gcd_of_n_numbers::gcd(array_1) == -1);
98+
assert(math::gcd_of_n_numbers::gcd(array_2) == 1);
99+
assert(math::gcd_of_n_numbers::gcd(array_3) == 2);
100+
assert(math::gcd_of_n_numbers::gcd(array_4) == 6);
101+
assert(math::gcd_of_n_numbers::gcd(array_5) == 100);
102+
assert(math::gcd_of_n_numbers::gcd(array_6) == -1);
103+
assert(math::gcd_of_n_numbers::gcd(array_7) == 3450);
104+
assert(math::gcd_of_n_numbers::gcd(array_8) == 1);
105+
}
38106

39-
delete[] a;
40-
return 0;
107+
/**
108+
* @brief Main function
109+
* @return 0 on exit
110+
*/
111+
int main() {
112+
test(); // run self-test implementation
113+
return 0;
41114
}

0 commit comments

Comments
 (0)