Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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_Stack Overflow - Fatal编程技术网

Java 概率计算中的堆栈溢出异常

Java 概率计算中的堆栈溢出异常,java,stack-overflow,Java,Stack Overflow,我在做第14题时(注意:我不是在寻找项目Euler问题的解决方案),遇到了一个有趣的堆栈溢出异常 我的非概率方法工作得很好,但当我尝试用概率方法解决同样的问题时,我遇到了堆栈溢出异常。有趣的是,这种异常只发生了大约17%的时间。一千次运行产生了166个异常 我知道我的概率逻辑有缺陷,但我更感兴趣的是异常的原因和防止它们发生的方法。我是否只需要做一些内存管理,或者在使用一些变量后将它们设置为null?如果是的话,关键点是什么 代码如下: public class Problem14_Longest

我在做第14题时(注意:我不是在寻找项目Euler问题的解决方案),遇到了一个有趣的堆栈溢出异常

我的非概率方法工作得很好,但当我尝试用概率方法解决同样的问题时,我遇到了堆栈溢出异常。有趣的是,这种异常只发生了大约17%的时间。一千次运行产生了166个异常

我知道我的概率逻辑有缺陷,但我更感兴趣的是异常的原因和防止它们发生的方法。我是否只需要做一些内存管理,或者在使用一些变量后将它们设置为null?如果是的话,关键点是什么

代码如下:

public class Problem14_LongestCollatzSequence {

    private static final int STARTING_CHAIN_LENGTH = 1;
    private static final int PROBABLY_RIGHT = 100000;

    /**
     * Calculate and return the Collatz sequence of a given number.
     *
     * @param number The number for which the Collatz sequence is to be
     * calculated.
     * @param chainlength The length of the chain for the number. This should
     * start with an initial value of 1.
     * @return The Length of the Collatz sequence.
     */
    private static int getChainLength(long number, int chainlength) {
        // All chains should end with 1.
        if (number != 1) {
            // If the number is even, halve the number, otherwise multiply it by 3 and add 1.
            if (number % 2 == 0) {
                number = number / 2;
            } else {
                number = number * 3 + 1;
            }
            // Call this function again.
            return getChainLength(number, ++chainlength);
        }
        // Return the length of the chain.
        return chainlength;
    }

    /**
     * Determine and return the number below a maximum value that will result in
     * the longest Collatz chain.
     *
     * @param maxStartingNumber The maximum value (exclusive) of the numbers
     * that will be tested.
     * @return The number that will produce the longest Collatz sequence in the
     * given range.
     */
    private static int calculateLongestChain(int maxStartingNumber) {
        Random random = new Random();
        int probabilityCounter = 0;
        int currentChainNumber = 0;
        int longestChainNumber = 0;
        int currentChainLength = 0;
        int longestChainLength = 0;

        // Get the chain length of random numbers until a certain number of unsuccsessful attempts have been made.
        while (probabilityCounter <= PROBABLY_RIGHT) {
            currentChainNumber = random.nextInt(maxStartingNumber);
            currentChainLength = getChainLength(currentChainNumber, STARTING_CHAIN_LENGTH);
            // If the current chain-length is bigger than the previously calculated one, reset the counter and update the chain number, otherwise increase the counter.
            if (currentChainLength > longestChainLength) {
                probabilityCounter = 0;
                longestChainLength = currentChainLength;
                longestChainNumber = currentChainNumber;
            } else {
                ++probabilityCounter;
            }
        }
        return longestChainNumber;
    }

    private static int calculateLongestChainNP(int maxStartingNumber) {
        // Non-probabilistic way to calculate the longest Collatz sequence.
        int currentChainLength = 0;
        int longestChainLength = 0;
        int longestChainNumber = 0;
        // Simply loop through all the numbers in the range to calculate the one resulting in the longest sequence.
        for (int i = 1; i < maxStartingNumber; i++) {
            currentChainLength = getChainLength(i, STARTING_CHAIN_LENGTH);
            if (currentChainLength > longestChainLength) {
                longestChainLength = currentChainLength;
                longestChainNumber = i;
            }
        }
        return longestChainNumber;
    }

    public static void main(String[] args) {
        int exceptionCount = 0;
        for (int count = 0; count < 1000; count++) {
            try {
                int testNumber = 1000000;
                System.out.println("Probabilistic answer: " + calculateLongestChain(testNumber));
                System.out.println("Non-probabilistic answer: " + calculateLongestChainNP(testNumber) + "\n");
            } catch (java.lang.StackOverflowError soe) {
                exceptionCount++;
                System.err.println(soe + "\n");
            }
        }
        System.out.println("Exception count: " + exceptionCount);
    }
}
公共类问题14_LongestCollatzSequence{
私有静态最终整数起始链长度=1;
私有静态最终整数可能为100000;
/**
*计算并返回给定数字的Collatz序列。
*
*@param number要为其创建Collatz序列的编号
*算计的。
*@param chainlength编号的链的长度。这应该是
*从初始值1开始。
*@返回Collatz序列的长度。
*/
私有静态int getChainLength(长数字,int chainlength){
//所有链条应以1结尾。
如果(数字!=1){
//如果数字为偶数,则将其减半,否则将其乘以3并加1。
如果(数字%2==0){
数量=数量/2;
}否则{
编号=编号*3+1;
}
//再次调用此函数。
返回getChainLength(数字,++chainlength);
}
//返回链的长度。
返回链长度;
}
/**
*确定并返回低于最大值的数字,该值将导致
*最长的项圈链。
*
*@param maxStartingNumber数字的最大值(不包括)
*这将受到考验。
*@返回将在列表中生成最长Collatz序列的数字
*给定范围。
*/
私有静态int-calculateLongestChain(int-maxStartingNumber){
随机=新随机();
int-probabilityCounter=0;
int currentChainNumber=0;
int longestChainNumber=0;
int currentChainLength=0;
int longestChainLength=0;
//获取随机数的链长度,直到进行了一定数量的失败尝试。
while(概率计数器最长链长){
概率计数=0;
longestChainLength=currentChainLength;
longestChainNumber=currentChainNumber;
}否则{
++概率计数器;
}
}
返回最长链号;
}
专用静态int-calculateLongestChainNP(int-maxStartingNumber){
//计算最长Collatz序列的非概率方法。
int currentChainLength=0;
int longestChainLength=0;
int longestChainNumber=0;
//只需循环遍历范围内的所有数字,即可计算出产生最长序列的数字。
对于(int i=1;ilongestChainLength){
longestChainLength=currentChainLength;
最长链号=i;
}
}
返回最长链号;
}
公共静态void main(字符串[]args){
int exceptionCount=0;
对于(int count=0;count<1000;count++){
试一试{
int testNumber=1000000;
System.out.println(“概率答案:”+计算长度链(testNumber));
System.out.println(“非概率答案:“+calculateLongestChainNP(testNumber)+”\n”);
}catch(java.lang.StackOverflowerr soe){
exceptionCount++;
System.err.println(soe+“\n”);
}
}
System.out.println(“异常计数:+exceptionCount”);
}
}

我也想提供完整的输出,但这超出了字符限制。

您将在stackoverflow异常中看到异常的原因。在这种情况下,递归太多,您将通过stacktrace中重复的stackframes看到它


试着让你的算法迭代而不是递归,你的问题就解决了

您的递归太深了。您可以使用
-xss4096m
增加JVM上的调用堆栈,但这是蛮力。在
getChainLength()
中使用
while
循环,而不是递归,要更加优雅:


getChainLength()
中使用while循环而不是递归-这将加快速度并释放大量堆栈。
private static int getChainLength(long number, int chainlength) {
        // All chains should end with 1.
        while (number != 1) {
            // If the number is even, halve the number, otherwise multiply it by 3 and add 1.
            if (number % 2 == 0) {
                number = number / 2;
            } else {
                number = number * 3 + 1;
            }
            // Call this function again.
            ++chainlength;
        }
        // Return the length of the chain.
        return chainlength;
    }