Discuss / Python / 内容有误

内容有误

Topic source

尾递归调用时,如果做了优化,栈不会增长,因此,无论多少次调用也不会导致栈溢出。

这里不是说不增长,而是线性增长,我亲测java,可看了很多篇博客

    private static BigInteger fact(BigInteger n) {
        return fact_iter(n, BigInteger.ONE);
    }

    private static BigInteger fact_iter(BigInteger n, BigInteger i) {
        if (n.compareTo(BigInteger.ONE) == 0) {
            return i;
        }
        return fact_iter(n.subtract(BigInteger.ONE), n.multiply(i));
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        System.out.println(fact(BigInteger.valueOf(1000)));
        }

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

你用scheme测一下


  • 1

Reply