java - 查找 A^5 + B^5 + C^5 所有可能值的算法?

标签 java algorithm

我正在尝试编写一种算法,当用户输入数字“N”时,该算法将找到 A^5 + B^5 + C^5 的所有可能值。

例如,如果 N=100 我想制作一个包含所有可能值的数组,其中数组中的每个槽都包含一个数字,该数字是通过插入 1-100 之间的数字找到的 A^5 + B^5 + C^5。因此,数组中的一个位置包含 (1^5 + 1^5 + 1^5) 中的 1。数组中的另一个位置包含 数字 355447518(从 19^5 + 43^5 + 46^5)。所以我的数组中将有 100^3 个元素。

public long[] possibleValues(int n)
{

    long[] solutionSet = new long[(int) Math.pow(n, 3)];

    for(int i=1;i<=n;i++)
    {
        solutionSet[i] = ((long) Math.pow(i, 5) + (long) Math.pow(i, 5) + (long) Math.pow(i, 5));

       //testing purposes
        System.out.println(i +"^5 " + "+" + i+"^5 " + "+" + i+"^5" + "=" + solutionSet[i]);
    }


    return solutionSet;
}

这就是我目前所拥有的,但我的问题是它没有进行 N 的所有排列。获得 N 的所有可能排列的最佳方法是什么?我是否使它变得比必要的更复杂?我将如何安排所有可能的(A、B、C)?

最佳答案

使用嵌套 for 循环:

index=0;
for (int i=1;i<=n;i++){
  for (int j=1;i<=n;j++){
    for (int k=1;i<=n;k++){

       solutionSet[index++] = ((long) Math.pow(i, 5) + (long) Math.pow(j, 5) + (long) Math.pow(k, 5));
    }
  }
}

您可以使用包含所有五次方的数组更快地计算所有次方,直到 N。

关于java - 查找 A^5 + B^5 + C^5 所有可能值的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008844/

相关文章:

java - XSD 错误 : cvc-complex-type. 2.4.a:发现以元素开头的无效内容

java - Spring-Integration-Kafka outbound-channel-adapter 发送消息

algorithm - 以下等式的时间复杂度是多少

c# - A*寻路..保存路径

algorithm - Patience Diff - 最后阶段对于非独特线路到底做了什么?

python - 为什么在 while 循环中尝试覆盖变量时会出现内存错误? (Python)

java - 如何在Java声音中连续改变音高?

java explode a line/string 像 php explode

java - 如何改善 jetty 响应时间?

algorithm - 最优算法