0

I'm trying to calculate the value of x in this equation:

(4 + 11111111/x)mod95 = 54

I've tried solving it using the top answer here: how to calculate reverse modulus However, it provides the lowest possible value for x (145, if helpful to anyone.) In addition, whenever 11111111/x is calculated, it removes any decimal places from the answer. Thanks!

Hiraeth
  • 1
  • 1
  • 3
    Note that in general there are an infinite number of solutions. What are you trying to find, if not the lowest solution? – 0x5453 Aug 30 '19 at 18:41
  • Whatever value of x makes the equation true. (x must be an integer) – Hiraeth Aug 30 '19 at 18:44
  • 1
    `bash` only has integer arithmetic, it always drops decimal places. – Barmar Aug 30 '19 at 18:56
  • 1
    What about brute force checking all x up to `11111111`? This gives you the results pretty quickly (`2, 55, 58, 74, 138, 205, 356...`) – Nico Schertler Aug 30 '19 at 19:25
  • 1
    What kind of division is that supposed to be? Is it modular arithmetic or just regular integer division? Because if you are trying to do modular arithmetic then this equation has no solution. – Tassle Aug 30 '19 at 21:57

1 Answers1

1

I guess you are referring to the bash code

(4 + 11111111 / x)  % 95 # == 54

Where / yields the int part of the division.

If you simplify that, an x that sattisfies this, also sattisfies:

(11111111 / x)  % 95 # == 50

And so also:

(11111111 / x) == 95 * i + 50  # for integer i

If we further look at the division that rounds towards the next lowest integer, we have

r= 11111111 % x
(11111111 - r)/x == 95*i + 50  # for integer i

(11111111 - r) == 5*(19*i + 10)*x # for integer i

So it can be rewritten as two conditions, which have to be met by any solution at once:

2222222 = (19*i + 10)*x
0 < 11111111 % x < x-1  # -1 because 11111111 % 5 == 1 and 11111111 % x < x

In other words, to find x you just need to check the two conditions for all divisors of 2222222.

In general, if you have questions like:

(a + b/x) mod m = c

transform it to

g=gcd(m, c-a)
c'= (c-a)/g
(b/x) mod m = g*c'
m= g*m'
b/x = g*c' + g*m'*i
r=  b%x
r'= b%g

# now search for an x that divides (b-r')/g
# and complies with the following conditions:
(b-r')/g = (c' + m'*i)*x
r' <= r < x-r'
jottbe
  • 4,228
  • 1
  • 15
  • 31