TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
extended_euclid_algorithm.cpp File Reference

GCD using [extended Euclid's algorithm] (https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) More...

#include <algorithm>
#include <iostream>
#include <cstdint>
Include dependency graph for extended_euclid_algorithm.cpp:

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.
 

Detailed Description

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.

Function Documentation

◆ extendedEuclid()

template<typename T , typename T2 >
void extendedEuclid ( T A,
T B,
T * GCD,
T2 * x,
T2 * y )

Implementation using recursive algorithm

Parameters
[in]Aunsigned
[in]Bunsigned
[out]GCDunsigned
[in,out]xsigned
[in,out]ysigned

Definition at line 71 of file extended_euclid_algorithm.cpp.

71 {
72 if (B > A)
73 std::swap(A, B); // Ensure that A >= B
74
75 if (B == 0) {
76 *GCD = A;
77 *x = 1;
78 *y = 0;
79 } else {
80 extendedEuclid(B, A % B, GCD, x, y);
81 T2 temp = *x;
82 *x = *y;
83 *y = temp - (A / B) * (*y);
84 }
85}
void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y)

◆ extendedEuclid_1()

template<typename T1 , typename T2 >
void extendedEuclid_1 ( T1 A,
T1 B,
T1 * GCD,
T2 * x,
T2 * y )

Implementation using iterative algorithm from Wikipedia

Parameters
[in]Aunsigned
[in]Bunsigned
[out]GCDunsigned
[out]xsigned
[out]ysigned

Definition at line 42 of file extended_euclid_algorithm.cpp.

42 {
43 if (B > A)
44 std::swap(A, B); // Ensure that A >= B
45
46 T2 s = 0, s0 = 1;
47 T2 t = 1, t0 = 0;
48 T1 r = B, r0 = A;
49
50 while (r != 0) {
51 T1 quotient = r0 / r;
52 update_step(&r, &r0, quotient);
53 update_step(&s, &s0, quotient);
54 update_step(&t, &t0, quotient);
55 }
56 *GCD = r0;
57 *x = s0;
58 *y = t0;
59}
void update_step(T *r, T *r0, const T2 quotient)

◆ main()

int main ( void )

Main function.

Definition at line 88 of file extended_euclid_algorithm.cpp.

88 {
89 uint32_t a, b, gcd;
90 int32_t x, y;
91 std::cin >> a >> b;
92 extendedEuclid(a, b, &gcd, &x, &y);
93 std::cout << gcd << " " << x << " " << y << std::endl;
94 extendedEuclid_1(a, b, &gcd, &x, &y);
95 std::cout << gcd << " " << x << " " << y << std::endl;
96 return 0;
97}
void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y)
int gcd(int num1, int num2)

◆ update_step()

template<typename T , typename T2 >
void update_step ( T * r,
T * r0,
const T2 quotient )
inline

function to update the coefficients per iteration

\[r_0,\,r = r,\, r_0 - \text{quotient}\times r\]

Parameters
[in,out]rsigned or unsigned
[in,out]r0signed or unsigned
[in]quotientunsigned

Definition at line 25 of file extended_euclid_algorithm.cpp.

25 {
26 T temp = *r;
27 *r = *r0 - (quotient * temp);
28 *r0 = temp;
29}