16
16
#include < cassert> // / for assert
17
17
#include < iostream> // / for IO operations
18
18
#include < optional> // / for std::optional
19
+
19
20
/* *
20
21
* @namespace math
21
22
* @brief Maths algorithms
@@ -42,6 +43,7 @@ int gcd_two(int x, int y) {
42
43
}
43
44
return gcd_two (y, x % y); // Euclidean method
44
45
}
46
+
45
47
/* *
46
48
* @brief Function to check if all elements in the array are 0
47
49
* @param a Array of numbers
@@ -58,6 +60,7 @@ bool check_all_zeros(const std::array<int, n>& a) {
58
60
* @param a Array of integers to compute GCD for
59
61
* @return std::optional<int> GCD of the numbers in the array or std::nullopt if undefined
60
62
*/
63
+
61
64
template <std::size_t n>
62
65
std::optional<int > gcd (const std::array<int , n>& a) {
63
66
// GCD is undefined if all elements in the array are 0
@@ -81,6 +84,7 @@ std::optional<int> gcd(const std::array<int, n>& a) {
81
84
* @brief Self-test implementation
82
85
* @return void
83
86
*/
87
+
84
88
static void test () {
85
89
std::array<int , 1 > array_1 = {0 };
86
90
std::array<int , 1 > array_2 = {1 };
@@ -100,6 +104,7 @@ static void test() {
100
104
assert (math::gcd_of_n_numbers::gcd (array_7) == 3450 );
101
105
assert (math::gcd_of_n_numbers::gcd (array_8) == 1 );
102
106
}
107
+
103
108
/* *
104
109
* @brief Main function
105
110
* @return 0 on exit
0 commit comments