Thinking in scala (4)----阶乘与尾递归

时间:2023-12-28 23:05:38

code1:

object factorial{
  def main(args:Array[String])={
    println(factorial(args(0).toInt))
  }

  def factorial(x:Int):Int =
    if (x==0) 1 else x * factorial(x-1)
}

这个实现的执行过程如下,比如我们计算4的阶乘factorial(4)

factorial(4)

--> if (4==0) 1 else 4 * factorial(4-1)

-->4*factorial(3)

-->4*(3*factorial(2))

-->4*(3*(2*factorial(1)))

-->4*(3*(2*(1*factorial(0))))

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

-->24

计算机在执行上述计算的过程中,需要用到“栈”,并且我们不难看到为了计算factorial(4),栈不断增大。

为了避免像这样的计算可能导致“栈溢出”,可以采用一个小技巧,就是将上述的递归转化为“尾”递归。

关于尾递归,可以参考:http://en.wikipedia.org/wiki/Tail_call

code2:

object factorial{
  def main(args:Array[String])={
    println(factorial(args(0).toInt,1))
  }

  def factorial(x:Int,result:Int):Int =
    if (x==0) result else factorial(x-1,x*result)
}

思路也很简单,不再赘述。

但是code2有个缺点,那就是factorial函数的参数多了一个:result,并且,每次调用factorial函数,都要给result传一个实参:1

因为将code2修改如下:

code3:

object factorial{
  def main(args:Array[String])={
    println(factorial(args(0).toInt))
  }

  def factorial(x:Int):Int ={
     def loop(x:Int,acc:Int):Int = {
       if(x==0) acc
       else loop(x-1,x*acc)
     }
     loop(x,1)
  }
}

The End..........