mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Create extended_euclid_algorithm.cpp (#759)
* Create extended_euclid_algorithm.cpp * Fix wihitespaces and fix if else space issues * Update extended_euclid_algorithm.cpp * updating DIRECTORY.md * Update extended_euclid_algorithm.cpp Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
0b6f8ff7d3
commit
de65e2b256
@ -96,6 +96,7 @@
|
||||
## Math
|
||||
* [Binary Exponent](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/binary_exponent.cpp)
|
||||
* [Eulers Totient Function](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/eulers_totient_function.cpp)
|
||||
* [Extended Euclid Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/extended_euclid_algorithm.cpp)
|
||||
* [Factorial](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/factorial.cpp)
|
||||
* [Fast Power](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fast_power.cpp)
|
||||
* [Greatest Common Divisor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/greatest_common_divisor.cpp)
|
||||
|
28
math/extended_euclid_algorithm.cpp
Normal file
28
math/extended_euclid_algorithm.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
// Finding coefficients of a and b ie x and y in gcd(a, b) = a * x + b * y
|
||||
// d is gcd(a, b)
|
||||
// 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.
|
||||
|
||||
int d, x, y;
|
||||
void extendedEuclid(int A, int B) {
|
||||
if (B == 0) {
|
||||
d = A;
|
||||
x = 1;
|
||||
y = 0;
|
||||
} else {
|
||||
extendedEuclid(B, A%B);
|
||||
int temp = x;
|
||||
x = y;
|
||||
y = temp - (A/B)*y;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a, b;
|
||||
std::cin >> a >> b;
|
||||
extendedEuclid(a, b);
|
||||
std::cout << x << " " << y << std::endl;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user