I have an equation y*8.57E-7 = x with the constraints:
x>0xshould be an integeryshould 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)
Now we know that:
857dividesx × 10^9;857and10^9are coprime.Hence by Gauss' lemma,
857dividesx.Hence there exists an integer
wsuch thatx = 857 × w.Similarly, we know that
10^9dividesy × 857, and10^9and857are coprime, hence by Gauss' lemma,10^9dividesy.So there exists integers
wandzsuch that:But then we must have:
So we have proven that if
x,yis a solution to your original equation, then there exists an integerzsuch thaty = z × 10^9andx = z × 857.Conversely, if there exists an integer
zsuch thaty = z × 10^9andx = z × 857, then you can easily check thaty × 8.57E-7 = x.As a conclusion, you can choose any integer
zthat you want, and then picky = z × 10^9andx = 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 givesy = 1000000000andx = 857.