英文:
Solving an equation such that the solution is inetger
问题
我有一个方程 y*8.57E-7 = x
,其约束条件如下:
x > 0
x
应该是一个整数y
应该是一个整数
这个问题可能可以通过动态规划来解决,但我在这方面的分析能力有限。对我来说,简单的解决方案是使用蛮力法。但是正如预期的那样,这需要太长时间,因为我们不知道解在哪个域中。我还尝试使用Excel的“求解”功能,但对于这个问题来说太抽象了(我无法添加第三个约束条件)。
那么,你们是如何快速解决这些问题的呢?我经常遇到这些问题,所以应该有一些方法可以快速解决,或者你可以写一些关于最佳实践、一些通用方法的内容。也许可以使用启发式方法来解决。
这是我的蛮力法代码:
for x in range(1, 1000000000):
y = int(1 / (x * 8.57E-7))
if x * y * 8.57E-7 == 1:
print(f"{x=}, {y=}")
(没有任何解决方案)
英文:
I have an equation y*8.57E-7 = x
with the constraints:
x>0
x
should be an integery
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)
答案1
得分: 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](https://en.wikipedia.org/wiki/Coprime_integers).
Hence by [Gauss' lemma](https://en.wikipedia.org/wiki/Euclid%27s_lemma#Formulations), `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`.
英文:
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
dividesx × 10^9
;857
and10^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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论