TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
gcd_of_n_numbers.cpp
Go to the documentation of this file.
1
14
#include <algorithm>
15
#include <array>
16
#include <cassert>
17
#include <iostream>
18
23
namespace
math
{
28
namespace
gcd_of_n_numbers
{
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
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
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
76
}
77
}
78
return
result;
79
}
80
}
// namespace gcd_of_n_numbers
81
}
// namespace math
82
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};
96
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
}
106
111
int
main
() {
112
test
();
// run self-test implementation
113
return
0;
114
}
test
void test()
Definition
caesar_cipher.cpp:100
math::gcd_of_n_numbers::gcd
int gcd(const std::array< int, n > &a)
Main program to compute GCD using the Euclidean algorithm.
Definition
gcd_of_n_numbers.cpp:64
math::gcd_of_n_numbers::gcd_two
int gcd_two(int x, int y)
Function to compute GCD of 2 numbers x and y.
Definition
gcd_of_n_numbers.cpp:35
math::gcd_of_n_numbers::check_all_zeros
bool check_all_zeros(const std::array< int, n > &a)
Function to check if all elements in the array are 0.
Definition
gcd_of_n_numbers.cpp:53
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
gcd_of_n_numbers
Compute GCD of numbers in an array.
math
for assert
math
gcd_of_n_numbers.cpp
Generated by
1.18.0