三,找到23组xyz值满足条件x^3+y^3=1+z^3

发布于 2024-11-27 05:34:59 字数 516 浏览 0 评论 0原文

现在我已经完成了查找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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱人如己 2024-12-04 05:34:59

这是一个工作代码:

  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 (因为它是整数除法) //use 1f/3f
  2. x 和 z 被交换 - 你想要 z > > x,而不是相反
  3. (long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3 // 使用 round

Here is a working code:

  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);
        }
     }
  }

Couple of problems in the old one:

  1. 1/3 == 0 (because it's integer division) //use 1f/3f
  2. x and z are swapped - you want z > x, not the other way around
  3. (long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3 // use round
轻许诺言 2024-12-04 05:34:59

如果你以另一种方式开始,则 X < Y< Z 通过将 Y 和 Z 增加到极限,您可以获得一些效率。一旦Z^3> X^3 + Y^3 + 1,由于三次函数的凹性,可以跳到下一个Y值。

这种 C# 实现在笔记本电脑上运行得非常快:

        UInt64 setsFound = 0;
        UInt64 xlim = 10000;
        UInt64 ylim = 1000000;
        UInt64 zlim = 10000000;

        //int ctr = 0;
        Console.WriteLine("The first 23 sets ordered by increasing x.");

        Parallel.For(1, (long)xlim, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
        //for (UInt64 i = 0; i < xlim; i++)
        {
            UInt64 x = (UInt64)i;
            UInt64 xCu = x * x * x;
            int zFails = 0;
            for (UInt64 y = x + 1; y < ylim; y++)
            {
                UInt64 yCu = y * y * y;
                zFails = 0;
                for (UInt64 z = y + 1; z < zlim & zFails < 1; z++)
                {
                    UInt64 zCu = z * z * z;
                    if (xCu + yCu - zCu == 1)
                    {
                        Console.WriteLine(String.Format("{0}: {1}^3 + {2}^3 - {3}^3 = 1", setsFound, x, y, z));
                        setsFound++;
                    }
                    else if (zCu > xCu + yCu - 1)
                    {
                        zFails++;
                    }
                }
            }
        }
        );

显然您可以取消并行化。另外,这里是该集合中的前 19 个元素(计算机仍在运行,我稍后会尝试发布最后 4 个元素):

output
(来源: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:

        UInt64 setsFound = 0;
        UInt64 xlim = 10000;
        UInt64 ylim = 1000000;
        UInt64 zlim = 10000000;

        //int ctr = 0;
        Console.WriteLine("The first 23 sets ordered by increasing x.");

        Parallel.For(1, (long)xlim, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
        //for (UInt64 i = 0; i < xlim; i++)
        {
            UInt64 x = (UInt64)i;
            UInt64 xCu = x * x * x;
            int zFails = 0;
            for (UInt64 y = x + 1; y < ylim; y++)
            {
                UInt64 yCu = y * y * y;
                zFails = 0;
                for (UInt64 z = y + 1; z < zlim & zFails < 1; z++)
                {
                    UInt64 zCu = z * z * z;
                    if (xCu + yCu - zCu == 1)
                    {
                        Console.WriteLine(String.Format("{0}: {1}^3 + {2}^3 - {3}^3 = 1", setsFound, x, y, z));
                        setsFound++;
                    }
                    else if (zCu > xCu + yCu - 1)
                    {
                        zFails++;
                    }
                }
            }
        }
        );

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):

output
(source: yfrog.com)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文