python中斐波那契数列
by Pau Pavón
通过保罗·帕文(PauPavón)
The Fibonacci sequence is, by definition, the integer sequence in which every number after the first two is the sum of the two preceding numbers. To simplify:
根据定义,斐波那契数列是整数序列,其中前两个后的每个数字都是前两个数之和。 为了简化:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
0、1、1、2、3、5、8、13、21、34、55、89、144,...
It has many applications in mathematics and even trading (yes, you read that right: trading), but that’s not the point of this article. My goal today is to show you how you can compute any term of this series of numbers in five different programming languages using recursive functions.
它在数学甚至交易中都有许多应用(是的,您没看错:交易),但这不是本文的重点。 我今天的目标是向您展示如何使用递归函数以五种不同的编程语言计算该系列数字的任何项。
Recursive functions are those functions which, basically, call themselves.
递归函数是那些基本上会调用自己的函数。
I want to note that this isn’t the best method to do it — in fact, it could be considered the most basic method for this purpose. This is because the computing power required to calculate larger terms of the series is immense. The number of times the function is called causes a stack overflow in most languages.
我要指出,这不是最好的方法-实际上,可以将其视为实现此目的的最基本方法。 这是因为计算较大序列项所需的计算能力非常大。 在大多数语言中,调用函数的次数导致堆栈溢出。
All the same, for the purposes of this tutorial, let’s begin.
出于相同的目的,就本教程而言,我们开始吧。
First of all, let’s think about what the code is going to look like. It’ll include:
首先,让我们考虑一下代码的外观。 其中包括:
· A recursive function F (F for Fibonacci): to compute the value of the next term.
·递归函数F(斐波纳契数为F):计算下一项的值。
· Nothing else: I warned you it was quite basic.
·没有别的:我警告过你,这很基本。
Our function will take n as an input, which will refer to the nth term of the sequence that we want to be computed. So, F(4) should return the fourth term of the sequence.
我们的函数将以n作为输入,它将引用我们要计算的序列的第n个项。 因此,F(4)应该返回序列的第四项。
Let’s plan it. The code should, regardless the language, look something like this:
让我们计划一下。 无论使用哪种语言,代码都应如下所示:
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
function F(n) if n = 0
return 0 if n = 1
return 1 else
return F(n-1) + F(n-2)
Note: the term 0 of the sequence will be considered to be 0, so the first term will be 1; the second, 1; the third, 2; and so on. You get it.
注意:序列中的项0将被视为0,因此第一个项将是1; 第二个,1; 第三,2; 等等。 你懂了。
Let’s analyze the function for a moment. If it gets 0 as an input, it returns 0. If it gets 1, it returns 1. If it gets 2… Well, in that case it falls into the else statement, which will call the function again for terms 2–1 (1) and 2–2 (0). That will return 1 and 0, and the two results will be added, returning 1. Perfect.
让我们分析一下功能。 如果它的输入为0,则返回0。如果它为1,则返回1。如果它为2,那么……在这种情况下,它属于else语句,它将再次为函数2–1( 1)和2–2(0)。 那将返回1和0,并且将两个结果相加,返回1.完美。
Now you can see why recursive functions are a problem in some cases. Imagine you wanted the 100th term of the sequence. The function would call itself for the 99th and the 98th, which would themselves call the function again for the 98th and 97th, and 97th and 96th terms…and so on. It would be really slow.
现在您可以了解为什么在某些情况下递归函数会成为问题。 假设您想要序列的第100个术语。 该函数将自己调用第99位和第98位,而它们本身将再次调用该函数分别调用第98和97位,第97和96位……等等。 真的很慢。
But the good news is that it actually works!
但是好消息是它确实有效!
So let’s start with the different languages. I won’t give too much detail (actually, no detail at all) to make your reading experience better. There isn’t too much to detail anyways.
因此,让我们从不同的语言开始。 我不会提供太多细节(实际上根本没有任何细节),以使您的阅读体验更好。 无论如何,没有太多细节可言。
Let’s jump into it:
让我们跳进去:
Python (Python)
def F(n): if n == 0:
return 0 if n == 1:
return 1 else:
return F(n-1) + F(n-2)
def F(n): if n == 0:
return 0 if n == 1:
return 1 else:
return F(n-1) + F(n-2)
Swift (Swift)
func F(_ n: Int) -> Int { if n == 0 { return 0
} if n == 1 { return 1
} else { return F(n-1) + F(n-2)
}}
func F(_ n: Int) -> Int { if n == 0 { return 0
} if n == 1 { return 1
} else { return F(n-1) + F(n-2)
}}
JavaScript (JavaScript)
function F(n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
function F(n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
Java (Java)
public static int F(int n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
public static int F(int n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
C ++ (C++)
int F(int n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
int F(int n) { if(n == 0) { return 0;
} if(n == 1) { return 1;
} else { return F(n-1) + F(n-2);
}}
And that’s it. I chose these languages just based on popularity — or at least because these 5 are the most common ones that I use They’re in no particular order. They could be classified by syntax difficulty, in my opinion, from Python (easiest) to C++ (hardest). But that depends on your personal opinion and your experience with each language.
就是这样。 我只是根据流行程度选择了这些语言-或至少因为这5种语言是我使用的最常见语言,所以它们的排列顺序没有特定关系。 在我看来,可以按照语法难度对它们进行分类,从Python(最简单)到C ++(最困难)。 但这取决于您的个人意见和您对每种语言的经验。
I hope you liked this article and, if you have any questions/recommendations or just want to say hi, comment below!
我希望您喜欢这篇文章,如果您有任何疑问/建议,或者只是想打个招呼,请在下面评论!
翻译自: https://www.freecodecamp.org/news/the-fibonacci-sequence-in-5-different-programming-languages-1c6514c749e5/
python中斐波那契数列