How to calculate the inverse module

I now have one formula:

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

but how to calculate the inverse module to get the smallest "x" if I already know the result

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

i means the formula or logic to get x

+4
source share
5 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; } 

eg.

 // (53 + x) % 62 = 44 var res = ReverseModulus(62,53,44); // res = 53 // (2 + x) % 8 = 3 var res = ReverseModulus(8,2,3); // res = 1 
+9
source

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

(A + x) % B = C

You can do

(B + C - A) % B = x

+5
source

x = (44 - 53) % 62 should work?

 x = (44 - a) % length; 
+1
source

What about

 IEnumerable<int> ReverseModulo( int numeratorPart, int divisor, int modulus) { for(int i = (divisor + modulus) - numeratorPart; i += divisor; i <= int.MaxValue) { yield return i; } } 

Now I know that this answer is wrong, because it is not the smallest, and .First() fixed it.

+1
source

Who needs a computer? If 53 + x is comparable to 44, then modulo 62, then we know that for an integer k,

 53 + x + 62*k = 44 

Solving for x, we see that

 x = 44 - 53 - 62*k = -9 - 62*k 

It is clear that the smallest solutions are -9 (for k = 0) and 53 (for k = 1).

0
source

All Articles