TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
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.
Namespaces | |
namespace | math |
for assert | |
namespace | gcd_of_n_numbers |
Compute GCD of numbers in an array. | |
Functions | |
int | math::gcd_of_n_numbers::gcd_two (int x, int y) |
Function to compute GCD of 2 numbers x and y. | |
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. | |
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. | |
static void | test () |
Self-test implementation. | |
int | main () |
Main function. | |
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.
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.
a | Array of numbers |
Definition at line 53 of file gcd_of_n_numbers.cpp.
int math::gcd_of_n_numbers::gcd | ( | const std::array< int, n > & | a | ) |
Main program to compute GCD using the Euclidean algorithm.
a | Array of integers to compute GCD for |
Definition at line 64 of file gcd_of_n_numbers.cpp.
int math::gcd_of_n_numbers::gcd_two | ( | int | x, |
int | y ) |
Function to compute GCD of 2 numbers x and y.
x | First number |
y | Second number |
Definition at line 35 of file gcd_of_n_numbers.cpp.
int main | ( | void | ) |
Main function.
Definition at line 111 of file gcd_of_n_numbers.cpp.
|
static |
Self-test implementation.
Definition at line 87 of file gcd_of_n_numbers.cpp.