Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate reverse modulus

now I have one formula:

int a = 53, x = 53, length = 62, result;
result = (a + x) % length;

but how to calculate reverse modulus to get the smallest "x" if I known result already

(53 + x) % 62 = 44
//how to get x

i mean what's the formula or logic to get x

like image 561
Ivan Li Avatar asked Aug 31 '12 15:08

Ivan Li


2 Answers

private int ReverseModulus(int div, int a, int remainder)
{
   if(remainder >= div)
      throw new ArgumentException("Remainder cannot be greater than or equal to divisor");
   if(a < remainder)
      return remainder - a;
   return div + remainder - a;
}

e.g. :

// (53 + x) % 62 = 44
var res = ReverseModulus(62,53,44); // res = 53

// (2 + x) % 8 = 3
var res = ReverseModulus(8,2,3); // res = 1
like image 149
digEmAll Avatar answered Sep 20 '22 07:09

digEmAll


It may not be the X that was originally used in the modulus, but if you have

(A + x) % B = C

You can do

(B + C - A) % B = x

like image 36
Corey Ogburn Avatar answered Sep 18 '22 07:09

Corey Ogburn