-3

I have a formula like: (2x + 3×7 + 5×9 + 7×2) mod 11 = 4 How can I find x in Python? Trial and error is not allowed

STW
  • 1
  • 1
  • 1
    It's fairly easy, but has nothing to do with Python. – Alexey Veleshko Dec 25 '21 at 10:40
  • Does this answer your question? [Reverse Modulus Operator](https://stackoverflow.com/questions/10133194/reverse-modulus-operator) – Ibraheem Alyan Dec 25 '21 at 10:41
  • Do you mean this to be (2x + 3*7 + 5*9 + 7*2) = (2x + 80) mod 11 = 4 or are the numbers on the right powers of x ? (2x + 3×^7 + 5×^9 + 7×^2) mod 11 = 4. Those would be two very different problems. You can find that x=6 for the first form easily. The second one is trickier. – Alain T. Dec 25 '21 at 21:16
  • My problem is the first one. Can you tell me how to fix this? – STW Dec 26 '21 at 07:36

2 Answers2

1

Generally, there are an infinite number of x that can satisfy this equation, but there should be some constraints. You can, for example, do the following to find the smallest positive x.

M = 11
for x in range(M):
    m = (2*x + 3*7 + 5*9 + 7*2)%M
    if m==4:
        print(x)
        break
H.asadi
  • 19
  • 2
-1

I think, you can't find the inverse of the mod % function, because

The modulo calculates the remainder when dividing the given 2 numbers...

Thus for the inverse of modulo, you would've to pass in the remainder and get the 2 numbers i.e the dividend and the divisor, back.

Now how is that mathematically possible as there can be infinite dividends and divisors that give the same remainder. For example

11%10 = 1

11%5 = 1

6%5 = 1

Here, in all the cases, the remainder is 1, now if I pass 1 to a function, unless there is some relationship between the value passed and the value to be returned, the function can't return the required value, you can see it in the cases above that there is no relationship between (5, 6, 10, 11) and 1, even if the divisor is same the dividend is not, and even if the dividend is same the divisor is not.

Thus, theoretically, the inverse of modulo is NOT a function at all, since a function can either map only:-

  • one value in its domain to one value in its range i.e one-one(injection) or

  • many values in its domain to one value in its range i.e many-one.

MK14
  • 481
  • 3
  • 9