简体   繁体   English

Java析因程序中的递归

[英]Recursion in Java factorial program

What I know about recursion is that it is that function calls itself and it has base condition where it stops. 我对递归的了解是函数调用了自身,并且它具有停止的基本条件。 My professor wrote a program and he called that recursion is occurring in it and I said no, it's not. 我的教授写了一个程序,他说其中正在发生递归,而我说不,不是。 I am confused about it. 我对此感到困惑。 So am asking you to clear my confusion. 因此,请您清除我的困惑。 Program made by him is below in code: 他编写的程序在下面的代码中:

int fact(int n) {
    if (n == 0) {
        return 1;
    }

    for (int i = n; i >= 1; i--) {
        s1.push(i);
    }
    return Return_result();

}

int Return_result() {
    int f = 1;
    while (s1.top != -1) {
        f = f * s1.pop();
    }
    return f;
}

You are right, there is no recursion in the presented code. 没错,显示的代码中没有递归。 It is an iterative approach. 这是一种迭代方法。

Recursion is, if in f() method (in Java nomenclature) or a function (in general), you have a call to f() . 递归是,如果在f()方法(以Java命名法)或函数(通常)中,您可以调用f()

Recursive approach will look like this: 递归方法将如下所示:

int fact(int n) {
    return (n <= 1) ? 1 : fact(n - 1) * n;
}

Or, using tail-recursion (also known as "tail call"): 或者,使用尾递归 (也称为“尾调用”):

int fact(int n) {
    return factTail(n, 1);
}

int factTail(int n, int accumulator) {
    return (n <= 1) ? accumulator : factTail(n - 1, n * accumulator);
}

Currently, JVM doesn't perform tail-recursion optimization, but this can change : 当前,JVM不执行尾递归优化,但这可以更改

It's important to note that this isn't a bug in the JVM. 重要的是要注意,这不是JVM中的错误。 It's an optimization that can be implemented to help functional programmers who use recursion, which is much more common and normal in those languages. 此优化可以实现,以帮助使用递归的函数式程序员,这在那些语言中更为常见和正常。 I recently spoke to Brian Goetz at Oracle about this optimization, and he said that it's on a list of things to be added to the JVM, but it's just not a high-priority item. 我最近与Oracle的Brian Goetz谈到了这一优化,他说,它是要添加到JVM的事物的清单,但这并不是一个高优先级的事项。 For now, it's best to make this optimization yourself, if you can, by avoiding deeply recursive functions when coding a functional language on the JVM. 现在,如果可以的话,最好自己进行优化,避免在JVM上编码功能语言时避免深度递归的功能。

first i want to explain some facts: 首先,我想解释一些事实:

  1. Factorial is computed by the following formula : n! = n * n-1 * n-2 * n-3 * .....* 3 * 2 * 1 阶乘通过以下公式计算: n! = n * n-1 * n-2 * n-3 * .....* 3 * 2 * 1 n! = n * n-1 * n-2 * n-3 * .....* 3 * 2 * 1
  2. Stack works in LIFO = last in first out or FILO = first in last out 堆栈采用LIFO = last in first outFILO = first in last out LIFO = last in first out FILO = first in last out

so the code does the following it first puts all numbers from n to 1 in a stack then it pops each one and multiply it with the current result to get the factorial at the end 因此,代码执行以下操作,首先将所有从n到1的数字放入堆栈中,然后将每个数字弹出并与当前结果相乘,最后得到阶乘

and by the way that is the iterative version for factorial (non-recursive), the recursive version will be like the following 顺便说一下,它是阶乘(非递归)的迭代版本,递归版本将如下所示

int fact(int n)
{
    int result;

   if(n==1)
     return 1;

   result = fact(n-1) * n;
   return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM