斐波那契数列,为什么这个循环函数有效?
我正在阅读一本编程书,其中一个示例是关于斐波那契数的,以及循环函数如何找到第 n 个斐波那契数。
代码如下所示:
Int fib (int n)
{
If (n<3)
Return (1);
Else
Return (fib(n-1))+(fib(n-2))
}
现在这并不准确,因为我是从手机上输入的,并且我了解代码是如何工作的,它会调用自身直到返回 1,然后将返回值相加,直到得到序列中位置的正确斐波那契数。
所以我不需要代码方面的帮助。我真正需要帮助的是理解为什么这是有效的。将所有回报相加如何得出正确答案?
请有人解释一下为什么这是有效的。谢谢。这让我抓狂。
I'm going through a programming book and one of the examples is about fibonacci numbers, and how a recurring function finds the fibonacci number for the nth one along.
The code looks like this:
Int fib (int n)
{
If (n<3)
Return (1);
Else
Return (fib(n-1))+(fib(n-2))
}
Now this isn't exact because I'm typing from my phone and I have a understanding how the code is working, it's calling itself until it returns 1, then it's adds up the return values until you have the correct fibonacci number for the position in the sequence.
So I don't need help with the code. What I do need help with is understanding why this works. How does adding all the returns give the correct answer?
Please can someone explain why this is working. Thanks. It's driving me mad.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
递归是这样的:
妈妈给她讲了一个故事
小青蛙
妈妈给她讲了一个故事
小熊
妈妈给她讲了一个故事
小黄鼠狼……
来源
Recursion is like this:
mother told her a story about a
little frog,
mother told her a story about a
little bear,
mother told her a story about a
little weasel...
source
我建议了解递归是如何工作的。基本上 fib 函数使用较小的参数执行自身,直到参数降为 2,然后它只返回 1。
了解其工作原理的一种方法是在调试器中逐步运行它。
I would suggest to understand how recursion works. Basically fib function is executing itself with a smaller argument until argument comes down to 2, then it just returns 1.
One way to understand how it works is to run it in a debugger step-by-step.
斐波那契数被定义为前两个斐波那契数之和。这给出了以下结果:
因此,对于第三个数字(1 1 2),您将获取找到前一个数字的结果 - 即第二个(1 1 2)数字并将其相加到上一个之前的数字 - 即第一个 (1 1 2) 数字。
你还必须明白,程序需要先计算前面两个数字的值,然后才能计算出你想知道的数字。因此,它不断调用自己 - 使用相同的方法 - 直到它计算完所有内容。
A Fibonacci number is defined as the sum of the two preceding Fibonacci numbers. That gives the following:
So for the 3rd number (1 1 2) you would take the result of finding the previous - i.e. 2nd (1 1 2) number and add it to the number before the previous - i.e. 1st (1 1 2) number.
You also have to understand that the program needs to calculate the value of the two preceding numbers before it can calculate the number you want to know. Therefore it keeps calling itself - using the same method - until it has calculated everything.
这是斐波那契数列的定义。
第 n 个斐波那契数返回第 (n-1) 个和第 (n-2) 个斐波那契数之和。
因此,我们可以通过归纳推理证明,如果 fib(n-1) 和 fib(n-2) 给出有效的第 (n-1) 个和第 (n-2) 个斐波那契数,则 fib(n) = fib (n-1)+fib(n-2) 将是有效的第 n 个斐波那契数。
基本步骤是 fib(1) 和 fib(2) 是正确的(即 fib(1)=fib(2)=1)...
It's the definition of Fibonacci numbers.
n-th Fibonacci number returns the sum of (n-1)th and (n-2)th Fibonacci numbers.
So we can prove by inductive reasoning that, if fib(n-1) and fib(n-2) give the valid (n-1)-th and (n-2)-th Fibonacci number, fib(n) = fib(n-1)+fib(n-2) will be the valid n-th Fibonacci number.
The base step is that fib(1) and fib(2) are correct (that's it fib(1)=fib(2)=1)...
理解递归的技巧是理解堆栈。
我位于一个名为
main
的函数中的第 2 行,所有局部变量都存储在我的堆栈帧中:然后我调用
fib(3)
,因此计算机推送我的当前位置(EIP)到堆栈,然后为 fib 创建一个新的堆栈帧并将其添加到堆栈中。我只能访问顶部堆栈帧:在
fib
的第 4 行,它再次调用fib
,因此同样的情况再次发生:并且它一次又一次地执行此操作函数被递归调用。堆栈一直增长,直到有东西返回,此时,在 fib 的第 2 行,它返回 1。这会弹出顶部堆栈帧并丢弃它,然后将执行返回到保存的执行指针和程序从停止的地方继续
最终您最终会回到调用函数(或者当它变得太大时会出现堆栈溢出)。要记住的关键是,每次调用该函数时,它都会获得一个包含所有局部变量的新堆栈帧,并且保存您之前的位置。这就是递归。
主要问题是,在教人们递归时,每个人总是使用斐波那契数列,这意味着在一行上有两个递归函数调用。这是不必要的混乱,我相信你会同意的!
The trick to understanding recursion is understanding the stack.
I'm at line 2 in a function called
main
, all my local variables are stored in my stack frame:I then call
fib(3)
, so the computer pushes my current position (EIP) to the stack, then creates a new stack frame for fib and adds that on too. I can only ever access the top stack frame:On line 4 of
fib
, it callsfib
again, so the same happens again:And it does this again and again as the function is recursively called. The stack grows until something returns, at which point, at line 2 of
fib
, it returns 1. This pops the top stack frame and discards it, it then returns execution to the saved execution pointer and the program continues where it left offEventually you end up back in the calling function (or you get a stack overflow as it grows too large). The key thing to remember is that each time the function is called, it gets a new stack frame containing all your local variables, and your previous position is saved. That's recursion.
The main problem is that in teaching people recursion, everyone always uses the Fibonacci sequence which means having two recursive function calls on one line. This is unnecessarily confusing, as I'm sure you'll agree!
斐波那契数被定义为前两个斐波那契数之和。它们各自定义为前两个斐波那契数之和。等等,等等,直到达到1。明白吗?任何随机斐波那契数都可以定义为两个斐波那契数之和;这些可以递归地定义为两个斐波那契数之和等。也就是说,斐波那契数的定义基本上是递归的;也就是说,它的定义涉及它所定义的内容。
这些东西可能很棘手,但它对于理解递归和计算机科学非常基础。继续努力;它最终会点击。
A fibonacci number is defined as the sum of the two previous fibonacci numbers. Which are each defined as the sum of the two previous fibonacci numbers. Etcetera, etcetera, until you reach 1. Understand? Any random fibonacci number can be defined as the sum of two fibonacci numbers; those can be recursively defined as the sum of two fibonacci numbers, etc. That is, the definition of a fibonacci number is fundamentally recursive; that is, the definition of it involves what it defines.
This stuff can be tricky, but it's very fundamental to understanding recursion and computer science. Keep working on it; it'll click eventually.
这称为递归
It's called recursion
本视频教程应该可以让您更好地了解斐波那契递归的工作原理
[链接] http://www.schooltrainer.com/study-material/computer-science/stepping-through-recursive-fibonacci-function.html
This video tutorial should give you a better picture of how Fibonacci recursion works
[link] http://www.schooltrainer.com/study-material/computer-science/stepping-through-recursive-fibonacci-function.html
根据定义,斐波那契数列是该数列中前两个数字的总和(其中前两个数字是 0 和 1)。
因此,当您在一个位置获得斐波那契数列时,您可以将其重写为前两个斐波那契数列的总和。
使用递归,您可以执行此过程,直到“之前的两个斐波那契数”分别为 1 和 1(在此算法的情况下),然后继续将数字相加“备份”递归,直到返回到您的序列中的原始位置。
By definition, fibonacci numbers are the sum of the two previous numbers in the series (where the first two are 0 and 1).
So, when you get the fibonacci number at one location, you can re-write it as the sum of the previously two fibonacci numbers.
Using recursion, you go through this process until the the "previously two fibonacci numbers" are 1 and 1 (in the case of this algorithm), and then proceed to adding the numbers together "back up" the recursion until you get back to your original location in the sequence.