1

I have an equation y*8.57E-7 = x with the constraints:

  1. x>0
  2. x should be an integer
  3. y should be an integer

It might be solved with dynamic programming, but I am weak in analyzing it this way. For me the simple solution was to use brute force. But as expected it takes too much time, and it should be as we don't know in what domain the solution would lie. I also tried to use excel 'solve' but it is too abstract for this problem (I couldn't add 3rd constraint in it).

SO, what you guys do to solve these problems fast and in a better way. I usually counter these problems quite frequently, so there should be something to solve it fast or you can write anything on best practices and something or on some general appraoch. Maybe something using heuristics.

Here is my brute force code:

for x in range(1, 1000000000):
    y = int(1 / (x * 8.57E-7))
    if x * y * 8.57E-7 == 1:
        print(f"{x=}, {y=}")

(No solution whatsoever)

  • As a general rule, if you want to find exact integer solutions, then don't use floats. `8.57E-6` is a float in python, and division `/` returns a float. This results in approximations, and so `x * y * 8.57E-6` will be an approximation, and so the equality check `x * y * 8.57E-6 == 1` is not going to be very trustworthy (you're comparing an approximation with an exact valuel) – Stef Jun 22 '23 at 10:01
  • 1
    Edit the question. Thanks for pointing it out, – umair mughal Jun 22 '23 at 10:22

1 Answers1

2
y × 8.57E-7 = x
y × 8.57 × 10^-7 = x
y × 857 × 10^-9 = x
y × 857 = x × 10^9

Now we know that:

  • 857 divides x × 10^9;
  • 857 and 10^9 are coprime.

Hence by Gauss' lemma, 857 divides x.

Hence there exists an integer w such that x = 857 × w.

Similarly, we know that 10^9 divides y × 857, and 10^9 and 857 are coprime, hence by Gauss' lemma, 10^9 divides y.

So there exists integers w and z such that:

y = z × 10^9
x = w × 857

But then we must have:

y × 857 = x × 10^9
z × 10^9 × 857 = w × 857 × 10^9
z = w

So we have proven that if x,y is a solution to your original equation, then there exists an integer z such that y = z × 10^9 and x = z × 857.

Conversely, if there exists an integer z such that y = z × 10^9 and x = z × 857, then you can easily check that y × 8.57E-7 = x.

As a conclusion, you can choose any integer z that you want, and then pick y = z × 10^9 and x = z × 857, and that'll give you a solution to the equation; and all solutions to the equation can be picked this way.

If you only need one solution, then you can for instance pick z = 1, which gives y = 1000000000 and x = 857.

Stef
  • 13,242
  • 2
  • 17
  • 28
  • Thanks. I got how it works and it is intelligent, but it is also mathematical. So, I think you always need some reformulations to solve these problems. Although I can do these as long as it is simple arithmatic, but sometimes not being a mathematician, the reformulation could make some problems hard to solve. So is there any general approach - maybe something which uses heuristics to pop out the solution using some computation power. And sorry if it is not a right question, – umair mughal Jun 22 '23 at 10:20
  • 1
    @umairmughal That strongly depends on what you mean by "these problems". Some problems are simple, other problems can be much harder. If all your problems are ***linear*** equations in integers, then I guess you could rely on solvers for [integer linear programming](https://en.wikipedia.org/wiki/Integer_programming) – Stef Jun 22 '23 at 10:24
  • @umairmughal But perhaps the most important thing here is to remove all floating-point numbers from your equations. If you want integers, then first rewrite your equations with rationals, and then multiply both sides of the equation by the denominators to get rid of the denominators. This is what I did at the beginning of my answer: I went from `y × 8.57E-7 = x` to `y × 857 = x × 10^9` by multiplying both sides of the equation by the denominator `10^9`. – Stef Jun 22 '23 at 10:49
  • @umairmughal Another way to look at this equation without naming Gauss' lemma is to say that if you want to have `y × 857 / 10^9 = x` and `y` and `x` both integers, then the only obstacle is that `y × 857` should be divisible by `10^9`, and the easiest way to get that is to choose `y` a multiple of `10^9`. So for instance you pick `y = 10^9`. (Gauss' lemma states that actually this is the only way, `y` ***must*** be a multiple of `10^9`). – Stef Jun 22 '23 at 10:53