Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[] smallXGCD(int m, int n, int c){
- int[] res = xGCD(m, n, c); //a and b from xGCD.
- int[] reshom = homGenXGCD(m, n); //a' and b' from homGenXGCD.
- int[] temp = { res[0] + reshom[0] , res[1] + reshom[1] }; //current calculated modulus.
- int[] minmod = { res[0] + reshom[0] , res[1] + reshom[1] }; // minimum modulus calculated to the moment.
- int[] aux = { 0, 0 };
- int sign = 1; //Decides the while's direction.
- boolean significant = true;
- int counter = 1;
- while(significant){
- counter++;
- temp[0] = res[0] + sign*reshom[0]*counter;
- temp[1] = res[1] + sign*reshom[1]*counter;
- if((Math.abs(temp[0]) + Math.abs(temp[1]) ) < (Math.abs(minmod[0]) + Math.abs(minmod[1]))){
- minmod[0] = temp[0];
- minmod[1] = temp[1];
- }
- if((Math.abs(temp[0]) + Math.abs(temp[1]) ) == (Math.abs(minmod[0]) + Math.abs(minmod[1]))){
- if(minmod[0] > temp[0]){
- minmod[0] = temp[0];
- minmod[1] = temp[1];
- }
- }
- if((Math.abs(temp[0]) + Math.abs(temp[1])) > (Math.abs(aux[0]) + Math.abs(aux[1]))){
- if(sign == -1)
- significant = false;
- else{
- counter = 1;
- sign = -1;
- }
- }
- aux[0] = temp[0];
- aux[1] = temp[1];
- }
- return minmod;
- }
Add Comment
Please, Sign In to add comment