简体   繁体   English

Fibonacci递归如何在C#中工作?

[英]How Does Fibonacci Recursion Works in C#

I am trying hard to understand the Fibonacci code below: 我正在努力理解下面的Fibonacci代码:

private static int mtdFibonacci(int number)
    {
        if (number == 0) return 0; 
        if (number == 1) return 1; 

        return fncRecursive(number - 1) + fncRecursive(number - 2);
    }

Basically, I am having a hard time creating it as a function on how did an input of 7, equals to 13. Although the answer is correct since the 7th element of Fibonacci sequence is 13: 基本上,我很难创建它作为一个函数,如何输入7,等于13.尽管答案是正确的,因为Fibonacci序列的第7个元素是13:

1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
1    1   2   3   5  8   13  21  34   55

Now I am trying to replicate the code on my own paper on how the Fibonacci recursive work and how does c# solve it: 现在我试图在我自己的论文中复制关于Fibonacci递归工作的代码以及c#如何解决它:

if n is 7:
return F(7) = F(7-1) + F(7-2) 
return F(7) = F(6) + F(5)
return F(7) = [F(5) + F(4)] + [F(4) + F(3)]
return F(7) = {[F(4) + F(3)] + [F(3) + F(2)]} + {[F(3) + F(2)] + [F(2) + F(1)]}
return 15?

I tried to check online but there is no explanation on how does this correct recursive function for Fibonacci sequence works. 我尝试在线检查,但没有解释这个正确的Fibonacci序列的递归函数是如何工作的。 The code is correct and the output is correct but I cannot replicate it using on my paper sequence above. 代码是正确的,输出是正确的,但我无法使用上面的纸张序列复制它。 Like the input of 7 is 15 to me. 就像7的输入对我来说是15。

Here's a way that I like to explain recursion to beginners. 这是我喜欢向初学者解释递归的方式。

Let's imagine a slightly simpler "pseudo code" language version of your program: 让我们设想一个稍微简单的“伪代码”语言版本的程序:

f(n) => if n < 3 then 1 else f(n-1) + f(n-2)

That's not legal C#, but read it over and make sure you understand the parts here. 这不是合法的C#,但请仔细阅读并确保您理解这些部分。

Now we are going to play a little game just with text . 现在我们打算用文字玩一个小游戏。 The rules of the game are: 游戏规则是:

  • If we see f(some number) then we replace it with ( the if expression with all the n's changed to some number ) . 如果我们看到f(some number)那么我们将其替换为( the if expression with all the n's changed to some number )

We'll need more rules in a minute, but let's start with that one. 我们在一分钟内需要更多规则,但让我们从那个开始。 Suppose we have: 假设我们有:

f(5)

We follow the rule. 我们遵循规则。 We replace it with 我们用它替换它

(if 5 < 3 then 1 else f(5-1) + f(5-2))

Next rules: 下一条规则:

  • number < number is replaced with either true or false number < number替换为truefalse
  • number - number is replaced with their difference number - number被替换为差异
  • number + number is replaced with their sum. number + number用它们的总和代替。
  • (number) is replaced with number provided that there is no f before ( .f (number)被替换为number前提是之前没有f ( .f

OK, apply those rules: 好的,应用这些规则:

(if false then 1 else f(4) + f(3))

Final rule: 最终规则:

  • if false then X else Y is replaced with Y. if true then X else Y is replaced with X. if false then X else Y将替换为Y. if true then X else Y将替换为X.

Apply it: 适用它:

(f(4) + f(3))

Now apply the first rule again: 现在再次应用第一条规则:

((if 4 < 3 then 1 else f(4-1) + f(4-2)) + (if 3 < 3 then 1 else f(3-1) + f(3-2))

Keep applying rules: 继续适用规则:

((if false then 1 else f(3) + f(2)) + (if false then 1 else f(2) + f(1))
(f(3) + f(2)) + (f(2) + f(1))

Let's skip a few steps here. 我们在这里跳过几步。 You see that f(3) is going to be replaced with (f(2) + f(1)) , that f(2) and f(1) are going to be replaced with (1) , right? 你看到f(3)将被替换为(f(2) + f(1))f(2)f(1)将被替换为(1) ,对吧?

(((f(2) + f(1)) + (1)) + ((1) + (1))
(((f(2) + f(1)) + 1) + (1 + 1)
(((f(2) + f(1)) + 1) + (2)
(((f(2) + f(1)) + 1) + 2

Again, skip a few steps. 再次,跳过几个步骤。 If they're not clear then do them yourself by hand 如果他们不清楚,那么自己动手做

((((1) + (1)) + 1) + 2
(((1 + 1) + 1) + 2
(((2) + 1 ) + 2
((2 + 1)) + 2
((3)) + 2
(3) + 2
3 + 2
5

And we're done. 我们已经完成了。 By just a few simple rules of string substitution we have deduced that f(5) is equal to 5. 通过一些简单的字符串替换规则,我们推断出f(5)等于5。

You can think of function activation in general in simple functions like these as simply "what if I had the function body with all its formal parameters replaced with their argument values?" 您可以将这些简单函数中的函数激活视为简单的“如果我将函数体的所有形式参数替换为它们的参数值会怎么样?” If you think about it like that, then recursion becomes more straightforward. 如果你这样想,那么递归变得更加直截了当。

Of course that is not how this is really implemented by the runtime, and this ignores a lot of aspects of control flow, variable mutation, and so on. 当然,这不是运行时实际如何实现的,这忽略了控制流,变量变异等许多方面。 But as a way to understand recursion early in your career, I think it is a pretty good one. 但作为一种在职业生涯早期了解递归的方法,我认为这是一个相当不错的方法。

You just need to carefully expand your sequence, that is all, no magic here. 你只需要仔细扩展你的序列,这就是全部,没有魔法。

F(7) = F(6) + F(5)
     = F(5) + F(4) + F(4) + F(3)
     = F(4) + F(3) + F(3) + F(2) + F(3) + F(2) + F(2) + F(1)
     = F(3) + F(2) + F(2) + F(1) + F(2) + F(1) + F(2) + F(2) + F(1) + F(2) + F(2) + F(1)
     = 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
     = 13
{[F(4) + F(3)] + [F(3) + F(2)]} + {[F(3) + F(2)] + [F(2) + F(1)]}
=   3  +   2   +    2  +   1    +     2  +   1   +    1  +   1 = 13

The I'm reading your question is what the logic behind above algorithm. 我正在阅读你的问题是上面的算法背后的逻辑。 One thing I strong believe is "One size does not fit all", this is one good example of it. 我坚信的一件事是“一个尺寸不适合所有人”,这是一个很好的例子。

A quick solution to calculate nth Fibonacci number is recursively add numbers until the nth loop is finished. 计算第n个Fibonacci数的快速解决方案是递归地添加数字,直到第n个循环结束。 On the contrary the above algorithm is trying to deduce the nth Fibonacci number in reverse order. 相反,上述算法试图以相反的顺序推导出第n个斐波那契数。

在此输入图像描述

The Goal here is to find nth Fibonacci number which by definition must be summation of previous two numbers which in turn will be summation of previous two numbers. 这里的目标是找到第n个斐波纳契数,根据定义,它必须是前两个数的总和,而这两个数又是前两个数的总和。

Going by this definition you can see from the graph I've created that x must be summation of y = x - 1 and z = x - 2 and so. 按照这个定义你可以从我创建的图中看到,x必须是y = x - 1和z = x - 2的总和,所以。

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

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