11
11
*
12
12
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
13
13
*/
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
19
18
20
19
/* *
21
20
* @namespace math
@@ -34,14 +33,14 @@ namespace gcd_of_n_numbers {
34
33
* @return GCD of x and y via recursion
35
34
*/
36
35
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
45
44
}
46
45
47
46
/* *
@@ -51,65 +50,65 @@ int gcd_two(int x, int y) {
51
50
* @return 'False' if not all elements are 0
52
51
*/
53
52
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 ; });
57
56
}
57
+
58
58
/* *
59
- * @brief Main program to compute GCD by repeatedly using the Euclidean algorithm
59
+ * @brief Main program to compute GCD using the Euclidean algorithm
60
60
* @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
62
62
*/
63
-
64
63
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
+ }
70
69
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
78
76
}
79
- return result;
77
+ }
78
+ return result;
80
79
}
81
- } // namespace gcd_of_n_numbers
82
- } // namespace math
80
+ } // namespace gcd_of_n_numbers
81
+ } // namespace math
82
+
83
83
/* *
84
84
* @brief Self-test implementation
85
85
* @return void
86
86
*/
87
-
88
87
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 };
97
96
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 );
106
105
}
107
106
108
107
/* *
109
108
* @brief Main function
110
109
* @return 0 on exit
111
110
*/
112
111
int main () {
113
- test (); // run self-test implementation
114
- return 0 ;
112
+ test (); // run self-test implementation
113
+ return 0 ;
115
114
}
0 commit comments