Skip to content

Commit aa7bb7a

Browse files
authored
Update gcd_of_n_numbers.cpp
1 parent 0ecb6bd commit aa7bb7a

File tree

1 file changed

+97
-26
lines changed

1 file changed

+97
-26
lines changed

math/gcd_of_n_numbers.cpp

Lines changed: 97 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,112 @@
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
54
*
5+
* @details
6+
* The GCD of n numbers can be calculated by
7+
* repeatedly calculating the GCDs of pairs of numbers
8+
* i.e. gcd(a, b, c) = gcd(gcd(a, b), c)
9+
* Euclidean algorithm helps calculate the GCD of each pair of numbers efficiently
10+
*
611
* @see gcd_iterative_euclidean.cpp, gcd_recursive_euclidean.cpp
712
*/
8-
#include <iostream>
13+
#include <iostream> /// for IO operations
14+
#include <cassert> /// for assert
15+
#include <algorithm> /// for std::abs
916

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`
17+
/**
18+
* @namespace math
19+
* @brief Maths algorithms
20+
*/
21+
namespace math {
22+
/**
23+
* @namespace gcd_of_n_numbers
24+
* @brief Compute GCD of numbers in an array
25+
*/
26+
namespace gcd_of_n_numbers {
27+
/**
28+
* @brief Function to compute GCD of 2 numbers x and y
29+
* @param x First number
30+
* @param y Second number
31+
* @return GCD of x and y via recursion
32+
*/
33+
int gcd_two(int x, int y) {
34+
// base cases
35+
if (y == 0) return x;
36+
if (x == 0) return y;
37+
return gcd_two(y, x % y); // Euclidean method
38+
}
39+
/**
40+
* @brief Function to check if all elements in array are 0
41+
* @param a Array of numbers
42+
* @param n Number of elements in array
43+
* @return 'True' if all elements are 0
44+
* @return 'False' if not all elements are 0
45+
*/
46+
bool check_all_zeros(int a[], size_t n) {
47+
// Check for the undefined GCD cases
48+
int zero_count = 0;
49+
for (int i = 0; i < n; ++i) {
50+
if (a[i] == 0) {
51+
++zero_count;
52+
}
53+
}
54+
55+
// return whether all elements in array are 0
56+
return zero_count == n;
57+
}
58+
/**
59+
* @brief Main program to compute GCD using division algorithm
60+
* @param a Array of integers to compute GCD for
61+
* @param n number of integers in the array
62+
* @return GCD of the numbers in the array
1463
*/
15-
int gcd(int *a, int n) {
16-
int j = 1; // to access all elements of the array starting from 1
64+
int gcd(int a[], size_t n) {
65+
// GCD is undefined if all elements in the array are 0
66+
if (check_all_zeros(a, n))
67+
return -1; // since gcd is positive, use -1 to mark undefined gcd
68+
1769
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
70+
for(int i = 1; i < n; i++) {
71+
gcd = gcd_two(gcd, a[i]);
72+
if (std::abs(gcd) == 1)
73+
break; // if gcd is already 1, further computations still result in gcd of 1
2374
}
24-
return gcd;
75+
76+
return std::abs(gcd); // divisors can be negative, and we only want positive value
2577
}
78+
} // namespace gcd_of_n_numbers
79+
} // namespace math
2680

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];
81+
/**
82+
* @brief Self-test implementation
83+
* @return void
84+
*/
85+
static void test() {
86+
int array_1[1] = {0};
87+
int array_2[1] = {1};
88+
int array_3[2] = {0, 2};
89+
int array_4[3] = {-60, 24, 18};
90+
int array_5[4] = {100, -100, -100, 200};
91+
int array_6[5] = {0, 0, 0, 0, 0};
92+
int array_7[7] = {90, -120, 0, 135, 660, -280, 900};
93+
int array_8[7] = {90, -120, 0, 4000, 0, 0, 111};
3694

37-
std::cout << "GCD of entered n numbers:" << gcd(a, n) << std::endl;
95+
assert(math::gcd_of_n_numbers::gcd(array_1, 1) == -1);
96+
assert(math::gcd_of_n_numbers::gcd(array_2, 1) == 1);
97+
assert(math::gcd_of_n_numbers::gcd(array_3, 2) == 2);
98+
assert(math::gcd_of_n_numbers::gcd(array_4, 3) == 6);
99+
assert(math::gcd_of_n_numbers::gcd(array_5, 4) == 100);
100+
assert(math::gcd_of_n_numbers::gcd(array_6, 5) == -1);
101+
assert(math::gcd_of_n_numbers::gcd(array_7, 7) == 5);
102+
assert(math::gcd_of_n_numbers::gcd(array_8, 7) == 1);
103+
}
38104

39-
delete[] a;
105+
/**
106+
* @brief Main function
107+
* @return 0 on exit
108+
*/
109+
int main() {
110+
test(); // run self-test implementation
40111
return 0;
41112
}

0 commit comments

Comments
 (0)