Created
April 16, 2019 22:09
-
-
Save VladRez/e0121547762a48a656aab01610b71b4f to your computer and use it in GitHub Desktop.
extended euclidean algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int egcd(int a, int b, int *x, int *y) { | |
if (a == 0) { | |
*x = 0; | |
*y = 1; | |
return b; | |
} | |
int x1, y1; | |
int gcd = egcd(b%a, a, &x1, &y1); | |
*x = y1 - (b / a) * x1; | |
*y = x1; | |
return gcd; | |
} | |
int main() { | |
int x, y; | |
int a = 35, b = 15; | |
int g = egcd(a, b, &x, &y); | |
printf("gcd = %d, x = %d, y = %d\n", g, x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment