This program aims at calculating the GCD of n numbers. More...
#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
Go to the source code of this file.
This program aims at calculating the GCD of n numbers.
The GCD of n numbers can be calculated by repeatedly calculating the GCDs of pairs of numbers i.e. \(\gcd(a, b, c)\) = \(\gcd(\gcd(a, b), c)\) Euclidean algorithm helps calculate the GCD of each pair of numbers efficiently
Definition in file gcd_of_n_numbers.cpp.
◆ check_all_zeros()template<std::size_t n>
bool math::gcd_of_n_numbers::check_all_zeros ( const std::array< int, n > & a )Function to check if all elements in the array are 0.
Definition at line 53 of file gcd_of_n_numbers.cpp.
53 {
54
55 return std::all_of(a.begin(), a.end(), [](int x) { return x == 0; });
56}
◆ gcd()template<std::size_t n>
int math::gcd_of_n_numbers::gcd ( const std::array< int, n > & a )Main program to compute GCD using the Euclidean algorithm.
Definition at line 64 of file gcd_of_n_numbers.cpp.
64 {
65
67 return -1;
68 }
69
70
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;
76 }
77 }
79}
uint64_t result(uint64_t n)
bool check_all_zeros(const std::array< int, n > &a)
Function to check if all elements in the array are 0.
◆ gcd_two() int math::gcd_of_n_numbers::gcd_two ( int x, int y )Function to compute GCD of 2 numbers x and y.
Definition at line 35 of file gcd_of_n_numbers.cpp.
35 {
36
37 if (y == 0) {
38 return x;
39 }
40 if (x == 0) {
41 return y;
42 }
43 return gcd_two(y, x % y);
44}
◆ main()Main function.
Definition at line 111 of file gcd_of_n_numbers.cpp.
111 {
113 return 0;
114}
static void test()
Self-test implementation.
◆ test()Self-test implementation.
Definition at line 87 of file gcd_of_n_numbers.cpp.
87 {
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
105}
int gcd(const std::array< int, n > &a)
Main program to compute GCD using the Euclidean algorithm.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4