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中斐波那契數列