Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算大数的置换_Java_Permutation - Fatal编程技术网

Java 计算大数的置换

Java 计算大数的置换,java,permutation,Java,Permutation,我使用这段java代码试图计算20的排列: (这只是用于计算置换的代码部分) int[]myArray=newint[20]; for(int i=0;i

我使用这段java代码试图计算20的排列: (这只是用于计算置换的代码部分)

int[]myArray=newint[20];
for(int i=0;i
但是,当然没有足够的记忆来做这件事


有什么建议吗?

大小为20的排列数是20的阶乘,结果大约是10^18。8GB的ram大约是8*10^9字节,正如您所理解的,这远低于存储10^18中所有可能的排列所需的存储量

这些排列中会有2432902008176640000个

如果每秒可以输出100000个排列,则需要 2432902008176640000/100000/60/60/24/365=771468年


100M每秒的排列速度更快,只需771年

内存更少的方法将涉及根据需要计算固定但可管理的排列数量。因此,您需要一个类似getNextPermutate()的方法

我从Guava的源代码Collection2.java中找到了该方法(您当前在代码中使用的方法):

你有

@Override
    protected List<E> computeNext() {
      if (nextPermutation == null) {
        return endOfData();
      }
      ImmutableList<E> next = ImmutableList.copyOf(nextPermutation);
      calculateNextPermutation();
      return next;
    }

    void calculateNextPermutation() {
      int j = findNextJ();
      if (j == -1) {
        nextPermutation = null;
        return;
      }

      int l = findNextL(j);
      Collections.swap(nextPermutation, j, l);
      int n = nextPermutation.size();
      Collections.reverse(nextPermutation.subList(j + 1, n));
}
@覆盖
受保护列表computeNext(){
if(nextPermutate==null){
返回endOfData();
}
ImmutableList next=ImmutableList.copyOf(nextPermutate);
calculateNextPermutation();
下一步返回;
}
void calculateNextPermutation(){
int j=findNextJ();
如果(j==-1){
nextPermutate=null;
回来
}
int l=findNextL(j);
集合交换(下一个术语,j,l);
int n=nextPermutate.size();
Collections.reverse(nextpermutate.subList(j+1,n));
}
你可以调用这个计算,比如说一次调用100或1000次,然后存储结果


就您而言,您必须在这里或那里分叉源代码并修改方法调用,以使其符合您的要求。

为程序分配更多内存?java-Xmx15g-jar(这还不够,我想可能代码中缺少一些内容,或者有更好的方法)您知道,我认为并行性可以工作的数组将有2432902008176640000个排列,但可能内存仍然是一个问题。我想这可能会对你有所帮助。事实上我知道,但是,没有办法做这样的计算(您不能将它们存储在数组/列表中。您可能会想出一些聪明的解决方案,例如在任何给定时刻只存储一个置换的迭代器,并使用.next()方法获取下一个置换,依此类推,这样您就不必同时存储所有置换。或者您可以购买44254229TB内存:)(= 2432902008176640000*20 / 1024 / 1024 / 1024 / 1024 )
@Override
    protected List<E> computeNext() {
      if (nextPermutation == null) {
        return endOfData();
      }
      ImmutableList<E> next = ImmutableList.copyOf(nextPermutation);
      calculateNextPermutation();
      return next;
    }

    void calculateNextPermutation() {
      int j = findNextJ();
      if (j == -1) {
        nextPermutation = null;
        return;
      }

      int l = findNextL(j);
      Collections.swap(nextPermutation, j, l);
      int n = nextPermutation.size();
      Collections.reverse(nextPermutation.subList(j + 1, n));
}