/*EXTENDED EUCLIDEN ALGORITHM FOR FIND GCD OF TWO NUMBERS */ #include < stdio.h > main(){ int m,b,a1,a2,a3,b1,b2,b3,gcd_MB,invrs_b,q,t1,t2,t3; char ch='a'; while(ch!='q'){ printf("ENTER M OR mod M= : "); scanf("%d",&m); printf("ENTER B = : "); scanf("%d",&b); /*A1,A2,A3 <---(1,0,M)*/ a1=1; a2=0; a3=m; /*B1,B2,B3 <---(0,1,B)*/ b1=0; b2=1; b3=b; while(1){ /*if B3=0 return A3=GCD(M,B) and B has no inverse */ if(b3==0){ gcd_MB=a3; invrs_b=0; break; } /*if B3=1 return B3=GCD(M,B) and B2=B(inverse) mod M*/ if(b3==1){ gcd_MB=b3; invrs_b=1; break; } /* Q=A3/B3 */ q=a3/b3; /*(T1,T2,T3) <--- [(A1-QB1),(A2-QB2),(A3-QB3)] */ t1=a1-q*b1; t2=a2-q*b2; t3=a3-q*b3; /*(A1,A2,A3) <--- (B1,B2,B3) */ a1=b1; a2=b2; a3=b3; /*(B1,B2,B3) <--- (T1,T2,T3) */ b1=t1; b2=t2; b3=t3; } if(invrs_b==0){ printf("\nGCD(B,M) = : %d",gcd_MB); printf("\n %d HAS NO MULTIPLICATIVE INVERSE IN mod %d \n",b,m); } if(invrs_b==1) { /*FIND THE MULTIPLICATIVE INVERSE OF B IN mod M by traditional method......*/ int c=1; while(((b*c)%m)!=1) { c++; if(c>m) break; } /*FIND MULTIPLICATIVE INVERSE OF B IN mod M BY value of b2 int c=1; while(c%m!=b2){ c++; if(c>m) break; }*/ if(c < m) { printf("\nGCD(B,M) = : %d",gcd_MB); printf("\nMULTIPLICATIVE INVERSE OF %d = %d in mod %d\n",b,c,m); } else { printf("\nGCD(B,M) = : %d",gcd_MB); printf("\n %d HAS NO INVERSE IN mod %d\n",b,m); }} scanf("%c",&ch); } }
Compile in LINUX environment by: Compile : gcc -o eea eea.c Run :./eea here gcc is a c/c++ compiler and eea.c is a c sourcecode file.
No comments:
Post a Comment