gpt4 book ai didi

java - 三、求23组x y z值满足条件x^3+y^3=1+z^3

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:11 25 4
gpt4 key购买 nike

现在我已经完成了找到23组x y z值满足条件 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);
}
}
}

但是我的代码效率很低,谁能帮我解决这个问题?

最佳答案

这是一个工作代码:

  int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long z = 1; setsFound < 23; z++) {
for (long y = z - 1; y > 0; y--) {
long x = (long) Math.pow((1 + z * z * z - y * y * y), 1f/3f);
if(y <= x) break;
if (x * x * x == 1 + z * z * z - y * y *y) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}

旧版本中的几个问题:

  1. 1/3 == 0(因为是整数除法)//使用1f/3f
  2. x 和 z 互换 - 你想要 z > x,而不是相反
  3. (long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3//使用舍入

关于java - 三、求23组x y z值满足条件x^3+y^3=1+z^3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6908385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com