PHP中一个数字的递归和阶乘

时间:2014-01-22 10:15:43

标签: php recursion

<?php  
function factorial_of_a($n)  
{  
    if($n ==0)  
    {  
        return 1;  
    }
    else   
    {    
        return $n * factorial_of_a( $n - 1 );  
    }  
}  
print_r( factorial_of_a(5) );
?>  

我怀疑是:

return $n * factorial_of_a( $n - 1 ) ;

在此声明中 - 当$n = 5$n - 1 = 4时,结果为20。 但是当我运行它时,答案是怎么回事? 那么,120是正确的答案......我不明白它是如何工作的。我使用for - 循环代替,它工作正常。

4 个答案:

答案 0 :(得分:6)

factorial_of_a(5)

触发以下来电:

5 * factorial_of_a(5 - 1) ->
5 * 4 * factorial_of_a(4 - 1) ->
5 * 4 * 3 * factorial_of_a(3 - 1) ->
5 * 4 * 3 * 2 * factorial_of_a(2 - 1) ->
5 * 4 * 3 * 2 * 1 * factorial_of_a(1 - 1) ->
5 * 4 * 3 * 2 * 1 * 1

所以,答案是120。

考虑阅读维基百科上的recursive function文章。

另外,请阅读此相关主题: What is a RECURSIVE Function in PHP?


  

但是如何回答120?

好吧,此功能会使用$n - 1调用自身,而$n - 1不等于0。如果是,则函数实际将结果返回给程序。所以它不会立即返回结果,而参数大于0。它被称为递归的“终止条件”

答案 1 :(得分:4)

它会以这种方式工作..

- factorial_of_a(5); 
// now read below dry run code from the bottom for proper understanding
// and then read again from top

   - if n = 0 ; false // since [n = 5]
   - else n*factorial_of_a(n-1); [return 5 * 24] 
   // here it will get 24 from the last line since 4*6 = 24 and pass 
   // it to the value of n i.e. **5** here will make it **120**

     - if n = 0 ; false [n = 4] 
     - else n*factorial_of_a(n-1); [return 4 * 6] 
     // here it will get 6 from the last line since 3*2 = 6 and pass it 
     // to the value of n i.e. **4** here will make it **24**

         - if n = 0 ; false [n = 3] 
         - else n*factorial_of_a(n-1); [return 3 * 2] 
         // here it will get 2 from the last line since 2*1 = 2 and pass it 
         // to the value of n i.e. **3** here will make it **6**

             - if n = 0 ; false [n = 2] 
             - else n*factorial_of_a(n-1); [return 2 * 1] 
             // here it will get 1 from the last line since 1*1 = 1 and pass it 
             // to the value of n i.e. **2** here

                 - if n = 0 ; false [n = 1] 
                 - else n*factorial_of_a(n-1); [return 1 * 1] 
                 // here it will get 1 from the last line and pass it 
                 // to the value of n i.e. **1** here

                     - if n = 0 ; true // since [n = 0] now it will return 1
                     - return 1;

答案 2 :(得分:2)

要理解这一点,您需要明确递归的概念。 递归表示一次又一次地调用函数。 每个递归函数都有一个终止案例和一个递归案例。 终止案例告诉函数何时停止,递归案例再次调用函数本身。

在您的代码中, if condition $ n == 0标记终止案例,即如果数字等于0并且返回1,则不进行任何计算。 else部分是递归案例[$ n * factorial_of_a($ n-1)]

Now i will explain how it works for $n = 5 :

Since $n is not equal to 0 then else statement is executed which gives 5 *factorial_of_a(4);

Now factorial_of_a(4) is called which gives 4 * factorial_of_a(3);

Now factorial_of_a(3) is called which gives 3 * factorial_of_a(2);

Now factorial_of_a(2) is called which gives 2 * factorial_of_a(1);

Now factorial_of_a(1) is called which gives 1 * factorial_of_a(0);

Now factorial_of_a(0) is called which gives 1

所以基本上

factorial_of_a(5) = 5 * 4 * 3 * 2 * 1 = 120

因此结果!

希望它有所帮助!!

答案 3 :(得分:0)

你必须记住,他是一个递归 当你调用factorial_of_a(5)时,它会将其作为

执行
factorial_of_a(5)  

// 5 * 24
5 * factorial_of_a(4)

  // 4 * 6
  4 * factorial_of_a(3)

     // 3 * 2
     3 * factorial_of_a(2)

         // 2 * 1
         2 * factorial_of_a(1)

             //will return 1 since it is your base condition
             1 * factorial_of_a(0)