python斐波那契递归
Good day, learners! In this tutorial we are going to learn about Python Recursion and use it for fibonacci sequence generation. In previous tutorial we discussed about Python Function and Arguments.
祝您学习愉快! 在本教程中,我们将学习Python递归并将其用于斐波那契序列生成。 在先前的教程中,我们讨论了Python函数和参数 。
Python递归 (Python Recursion)
Functions that are implemented using recursion can be implemented using loops. But you have to know the basics of Python Recursion. Basically, when a thing is defined by itself, recursion occurs. You may have heard the name of scripting language PHP. The acronym of PHP is PHP Hypertext Preprocessor. This is an example of recursion. In python, recursion occurs when a function is defined by itself. The structure of a Python recursive function is given below
使用递归实现的功能可以使用循环来实现。 但是您必须了解Python递归的基础。 基本上,当事物由其自身定义时,就会发生递归。 您可能已经听说过脚本语言PHP的名称。 PHP的缩写是PHP超文本预处理器 。 这是递归的示例。 在python中,递归是在函数由其自身定义时发生的。 Python递归函数的结构如下
def recursive_function(arguments):
#check for the terminating condition
if breaking_condition == True :
#Calculate result
return result
#perform some operation with the arguments
#call the function itself to perform further operation
return recursive_function(arguments_for_further_operation)
Python Fibonacci系列 (Python Fibonacci Series)
Fibonacci series is basically a sequence. In that sequence, each number is sum of previous two preceding number of that sequence. Initial two number of the series is either 0 and 1 or 1 and 1. We will consider 0 and 1 as first two numbers in our example. So, the first few number in this series are
斐波那契数列基本上是一个序列。 在该序列中,每个数字是该序列的前两个在先数字的总和。 该序列的前两个数字为0和1或1和1。在我们的示例中,我们将0和1视为前两个数字。 因此,本系列的前几个数字是
We see that,
我们看到了
- 1st Fibonacci number = 0 (by assumption) 第一斐波那契数= 0(假设)
- 2nd Fibonacci number = 1 (by assumption) 第二斐波那契数= 1(假设)
- 3rd Fibonacci number = 1st + 2nd
= 0 + 1
= 1 第三斐波那契数= 1 + 2
= 0 + 1
= 1 - 4th Fibonacci number = 2nd + 3rd
= 1 + 1
= 2 第四斐波那契数=第二+第三
= 1 + 1
= 2 - 5th Fibonacci number = 3rd + 4th
= 1 + 2
= 3 第5个斐波那契数=第3 +第4
= 1 + 2
= 3 - 6th Fibonacci number = 4th + 5th
= 2 + 3
= 5 第六斐波那契数=第四+ 5
= 2 + 3
= 5 - So, nth Fibonacci number = (n-1)th Fibonacci + (n-2)th Fibonacci 因此,第n个斐波纳契数=第(n-1)个斐波那契+(n-2)个斐波那契
So, the code for implementing the Fibonacci function is given below.
因此,下面给出了实现斐波那契函数的代码。
def Fibonacci( pos ):
#check for the terminating condition
if pos <= 1 :
#Return the value for position 1, here it is 0
return 0
if pos == 2:
#return the value for position 2, here it is 1
return 1
#perform some operation with the arguments
#Calculate the (n-1)th number by calling the function itself
n_1 = Fibonacci( pos-1 )
#calculation the (n-2)th number by calling the function itself again
n_2 = Fibonacci( pos-2 )
#calculate the fibo number
n = n_1 + n_2
#return the fibo number
return n
#Here we asking the function to calculate 5th Fibonacci
nth_fibo = Fibonacci( 5 )
print (nth_fibo)
The above code will calculate the Fibonacci number using recursion technique. The following image will help you to understand the concept in more effective way. In this picture, the blue boxes are the calls of functions where the terminating conditions is met.
上面的代码将使用递归技术计算斐波那契数。 下图将帮助您更有效地理解该概念。 在此图中,蓝色框表示满足终止条件的函数调用。
Python递归的优势 (Advantages of Python Recursion)
Implementing something using recursion requires less effort. The code you wrote using recursion will be comparatively smaller than the code that is implemented by loops. Again, code that are written using recursion are easier to understand also.
使用递归实现某些工作需要更少的精力。 您使用递归编写的代码将比通过循环实现的代码要小。 同样,使用递归编写的代码也更容易理解。
Python递归的缺点 (Disadvantages of Python Recursion)
Recursion requires more function call. Each function call stores some state variable to the program stack. If your code requires too many function calls, it will consumes too much memory. So, there may be some possibilities of causing memory overflow if your code is not that much efficient.
递归需要更多的函数调用。 每个函数调用都将一些状态变量存储到程序堆栈中。 如果您的代码需要太多的函数调用,则会消耗太多的内存。 因此,如果您的代码效率不高,则可能会导致内存溢出。
Again it takes some time to call a function, if the task of the function is done, the it recall the parent function which also cause some time to re-execute the parent function from the previous state. So, recursive function consumes more time to perform it’s task.
再次调用一个函数需要花费一些时间,如果该函数的任务完成了,它将调用父函数,这也会导致一些时间从先前的状态重新执行父函数。 因此,递归函数会花费更多时间来执行其任务。
Furthermore, debugging a recursive function is more difficult in most of the cases.
此外,在大多数情况下,调试递归函数更困难。
So, you shouldn’t implement functions using recursion if it can be implemented without recursion. Recursive functions are not that much efficient comparing to that implementation using loop but easier to implement.
因此,如果可以在没有递归的情况下实现,则不应使用递归来实现函数。 与使用循环的实现相比,递归函数的效率不高,但更易于实现。
So, that’s all about Python recursion. If you have any query about recursion, please comment below.
所以,这一切都与Python递归有关。 如果您对递归有任何疑问,请在下面发表评论。
翻译自: https://www.journaldev.com/14271/python-recursion-fibonacci
python斐波那契递归