Skip to content

Commit 92b2940

Browse files
authored
Update gcd_of_n_numbers.cpp
1 parent b157b6f commit 92b2940

File tree

1 file changed

+52
-53
lines changed

1 file changed

+52
-53
lines changed

math/gcd_of_n_numbers.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
*
1212
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
1313
*/
14-
#include <algorithm> /// for std::abs
15-
#include <array> /// for std::array
16-
#include <cassert> /// for assert
17-
#include <iostream> /// for IO operations
18-
#include <optional> /// for std::optional
14+
#include <algorithm> /// for std::abs
15+
#include <array> /// for std::array
16+
#include <cassert> /// for assert
17+
#include <iostream> /// for IO operations
1918

2019
/**
2120
* @namespace math
@@ -34,14 +33,14 @@ namespace gcd_of_n_numbers {
3433
* @return GCD of x and y via recursion
3534
*/
3635
int gcd_two(int x, int y) {
37-
// base cases
38-
if (y == 0) {
39-
return x;
40-
}
41-
if (x == 0) {
42-
return y;
43-
}
44-
return gcd_two(y, x % y); // Euclidean method
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
4544
}
4645

4746
/**
@@ -51,65 +50,65 @@ int gcd_two(int x, int y) {
5150
* @return 'False' if not all elements are 0
5251
*/
5352
template <std::size_t n>
54-
bool check_all_zeros(const std::array<int, n>& a) {
55-
// Use std::all_of to simplify zero-checking
56-
return std::all_of(a.begin(), a.end(), [](int x) { return x == 0; });
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; });
5756
}
57+
5858
/**
59-
* @brief Main program to compute GCD by repeatedly using the Euclidean algorithm
59+
* @brief Main program to compute GCD using the Euclidean algorithm
6060
* @param a Array of integers to compute GCD for
61-
* @return std::optional<int> GCD of the numbers in the array or std::nullopt if undefined
61+
* @return GCD of the numbers in the array or std::nullopt if undefined
6262
*/
63-
6463
template <std::size_t n>
65-
std::optional<int> gcd(const std::array<int, n>& a) {
66-
// GCD is undefined if all elements in the array are 0
67-
if (check_all_zeros(a)) {
68-
return std::nullopt; // Use std::optional to represent undefined GCD
69-
}
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+
}
7069

71-
// divisors can be negative, we only want the positive value
72-
int result = std::abs(a[0]);
73-
for (std::size_t i = 1; i < n; ++i) {
74-
result = gcd_two(result, std::abs(a[i]));
75-
if (result == 1) {
76-
break; // Further computations still result in gcd of 1
77-
}
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
7876
}
79-
return result;
77+
}
78+
return result;
8079
}
81-
} // namespace gcd_of_n_numbers
82-
} // namespace math
80+
} // namespace gcd_of_n_numbers
81+
} // namespace math
82+
8383
/**
8484
* @brief Self-test implementation
8585
* @return void
8686
*/
87-
8887
static void test() {
89-
std::array<int, 1> array_1 = {0};
90-
std::array<int, 1> array_2 = {1};
91-
std::array<int, 2> array_3 = {0, 2};
92-
std::array<int, 3> array_4 = {-60, 24, 18};
93-
std::array<int, 4> array_5 = {100, -100, -100, 200};
94-
std::array<int, 5> array_6 = {0, 0, 0, 0, 0};
95-
std::array<int, 7> array_7 = {10350, -24150, 0, 17250, 37950, -127650, 51750};
96-
std::array<int, 7> array_8 = {9500000, -12121200, 0, 4444, 0, 0, 123456789};
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};
9796

98-
assert(math::gcd_of_n_numbers::gcd(array_1) == std::nullopt);
99-
assert(math::gcd_of_n_numbers::gcd(array_2) == 1);
100-
assert(math::gcd_of_n_numbers::gcd(array_3) == 2);
101-
assert(math::gcd_of_n_numbers::gcd(array_4) == 6);
102-
assert(math::gcd_of_n_numbers::gcd(array_5) == 100);
103-
assert(math::gcd_of_n_numbers::gcd(array_6) == std::nullopt);
104-
assert(math::gcd_of_n_numbers::gcd(array_7) == 3450);
105-
assert(math::gcd_of_n_numbers::gcd(array_8) == 1);
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);
106105
}
107106

108107
/**
109108
* @brief Main function
110109
* @return 0 on exit
111110
*/
112111
int main() {
113-
test(); // run self-test implementation
114-
return 0;
112+
test(); // run self-test implementation
113+
return 0;
115114
}

0 commit comments

Comments
 (0)