1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <stdio.h>
#include <math.h>
typedef struct solution_ {
int d;
int x;
int y;
}Solution;
int pgcd(int a, int b) {
return (b == 0) ? a : pgcd(b,a%b);
}
Solution euclide_etendu(int a, int b) {
Solution s;
Solution tmp;
if (b == 0) {
s.d = a;
s.x = 1;
s.y = 0;
return s;
}
else {
tmp = euclide_etendu(b,a%b);
s.d = tmp.d;
s.x = tmp.y;
s.y = tmp.x-(floor(a/b)*tmp.y);
return s;
}
}
int main(void) {
Solution s = euclide_etendu(5,49);
printf("pgcd(5,49) = %d = (5*%d) + (49*%d)\n",s.d,s.x,s.y);
return 0;
} |
Partager