三,找到23组xyz值满足条件x^3+y^3=1+z^3
现在我已经完成了查找23组xyz值满足条件x^3+y^3=1+z^3 & x
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long x = 1; setsFound < 23; x++) {
for (long z = x + 1; z<x*x; z++) {
long y = (long) Math.pow((1 + z*z*z - x*x*x), 1f/3f);
if (x * x * x == 1 + z * z * z - y * y *y && x<y && y<z) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
但是我的代码效率很低,有人可以帮我解决这个问题吗?
Now I have completed the finding 23 sets of x y z values satisfy the condition x^3+y^3=1+z^3 & x
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long x = 1; setsFound < 23; x++) {
for (long z = x + 1; z<x*x; z++) {
long y = (long) Math.pow((1 + z*z*z - x*x*x), 1f/3f);
if (x * x * x == 1 + z * z * z - y * y *y && x<y && y<z) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
But the code I have is very inefficient, can anyone help me to fix this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个工作代码:
旧代码中的几个问题:
Here is a working code:
Couple of problems in the old one:
如果你以另一种方式开始,则 X < Y< Z 通过将 Y 和 Z 增加到极限,您可以获得一些效率。一旦Z^3> X^3 + Y^3 + 1,由于三次函数的凹性,可以跳到下一个Y值。
这种 C# 实现在笔记本电脑上运行得非常快:
显然您可以取消并行化。另外,这里是该集合中的前 19 个元素(计算机仍在运行,我稍后会尝试发布最后 4 个元素):
(来源:yfrog.com)
If you start the other way, with X < Y < Z by incrementing Y and Z up to a limit, you can gain some efficiencies. Once Z^3 > X^3 + Y^3 + 1, you can skip to the next Y value due to the concavity of the cubic function.
This implementation in C# works pretty fast on a laptop:
Obviously you can take out the parallelization. Also, here are the first 19 elements in that set (computer is still running, I'll try to post the last 4 later):
(source: yfrog.com)