TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
GCD using [extended Euclid's algorithm] (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) More...
#include <algorithm>
#include <iostream>
#include <cstdint>
Go to the source code of this file.
Functions | |
template<typename T , typename T2 > | |
void | update_step (T *r, T *r0, const T2 quotient) |
template<typename T1 , typename T2 > | |
void | extendedEuclid_1 (T1 A, T1 B, T1 *GCD, T2 *x, T2 *y) |
template<typename T , typename T2 > | |
void | extendedEuclid (T A, T B, T *GCD, T2 *x, T2 *y) |
int | main () |
Main function. | |
GCD using [extended Euclid's algorithm] (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm)
Finding coefficients of a and b ie x and y in Bézout's identity
\[\text{gcd}(a, b) = a \times x + b \times y \]
This is also used in finding Modular multiplicative inverse of a number. (A * B)M == 1 Here B is the MMI of A for given M, so extendedEuclid (A, M) gives B.
Definition in file extended_euclid_algorithm.cpp.
void extendedEuclid | ( | T | A, |
T | B, | ||
T * | GCD, | ||
T2 * | x, | ||
T2 * | y ) |
Implementation using recursive algorithm
[in] | A | unsigned |
[in] | B | unsigned |
[out] | GCD | unsigned |
[in,out] | x | signed |
[in,out] | y | signed |
Definition at line 71 of file extended_euclid_algorithm.cpp.
void extendedEuclid_1 | ( | T1 | A, |
T1 | B, | ||
T1 * | GCD, | ||
T2 * | x, | ||
T2 * | y ) |
Implementation using iterative algorithm from Wikipedia
[in] | A | unsigned |
[in] | B | unsigned |
[out] | GCD | unsigned |
[out] | x | signed |
[out] | y | signed |
Definition at line 42 of file extended_euclid_algorithm.cpp.
int main | ( | void | ) |
Main function.
Definition at line 88 of file extended_euclid_algorithm.cpp.
|
inline |
function to update the coefficients per iteration
\[r_0,\,r = r,\, r_0 - \text{quotient}\times r\]
[in,out] | r | signed or unsigned |
[in,out] | r0 | signed or unsigned |
[in] | quotient | unsigned |
Definition at line 25 of file extended_euclid_algorithm.cpp.