通过算法寻找递归方程

时间:2015-04-18 12:09:56

标签: recursion complexity-theory

我想在这个算法中提供一些帮助

int two(int n)
{
   int sum = 0;
   int i;
   if n == 0
      return 7;
   for(i = 0; i <= n-1; i++)
      sum += two(i);
   return sum;
}

我试图找到递归方程,但我不知道如何。 有什么想法??

2 个答案:

答案 0 :(得分:2)

two(0) = 7

two(i) = two(0) + two(1) + ... + two(i-1) 

two(i) = Σ(Two(i)) with i = 0 to n-1

我希望这会有所帮助。这是获取递归方程式应遵循的步骤。

实施例:two(3)

two(3) = two (2) + two(1) + two(0)

two(2) = two(1) + two(0)

two(1) = two(0)

two(0) = 7

two(1) = 7

two(2) = 7 + 7

two(3) = 7 + 7 + (7 + 7)  = 28 

答案 1 :(得分:0)

你的递归方程是

two(n) = 7                         , n == 0
       = SUM( two(i) )[i=0..n-1]   , n > 0

在序列中写出几个术语,从0开始,我们得到

two(n)[n=0,1..] = 7, 7, 14, 28, 56, ...

那么你的递推方程就是

two(n) = 7                , n <= 1
       = two(n-1) * 2     , n > 1

然后是封闭式解决方案

two(n) = 7             , n == 0
       = 7 * 2^ ...    , n > 0