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

时间:2014-01-20 09:47:47

标签: java stack-overflow

当我遇到一个有趣的堆栈溢出异常时,我在Project Euler上做了问题14(注意:我不是在寻找Project 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);
    }
}

我也想提供完整的输出,但这让我超越了角色限制。

2 个答案:

答案 0 :(得分:1)

您将在stackoverflow异常中看到异常的原因。在这种情况下,它是递归太多,您将通过堆栈跟踪中的重复堆栈帧看到它。

尝试使您的算法迭代而不是递归,并解决您的问题。

答案 1 :(得分:1)

你的递归太深了。您可以使用-Xss 4096m增加JVM上的调用堆栈,但这是强力的。更优雅,并使用while循环而不是getChainLength()中的递归:

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