unsigned int getMultiplicativeInverseUsingExtendedEuclides(unsigned int x,unsigned int y){ int *ee=extendedEuclides(x,y); if(ee[1]<0){ return y-((ee[1]*-1)%y); } return (ee[1]%y); }
int main ( void ) { scanf ( "%d %d", & first, & second ); result = extendedEuclides ( first, second ); // Zakładam, że first > second printf ( "%d %d %d\n", result . x, result . y, result . gcd ); return 0; }
Gcd extendedEuclides ( int first, int second ) { Gcd result; int tmp; result . gcd = second; if ( first % second == 0 ) return result; result = extendedEuclides ( second, first % second ); tmp = result . x; result . x = result . y; result . y = tmp - result . y * ( first / second ); return result; }
int *extendedEuclides(unsigned int x, unsigned int y){//this function return array that contain multiplicative inverse of x in Z/y and viseversa multiplicative inverse of y in Z/x int quotient=x/y; unsigned int remainder=x%y; int *last=(int *)malloc(sizeof(int)*4);//here was exprecion of inverse multiplicative (1=ax+by, where a is inverse multiplicative of x in Z/y and b is inverse multiplicative of y in Z/x) if(remainder==1){ last[0]=x; last[1]=1; last[2]=y; last[3]=-1*quotient; return last; } last=extendedEuclides(y,remainder); int auxiliary; last[0]=x; auxiliary=last[1]; last[1]=last[3]; last[2]=y; last[3]=last[3]*-1*quotient+auxiliary; return last; }