python中斐波那契數列_斐波那契數列–在Python,JavaScript,C ++,Java和Swift中進行了解釋...

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390314.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390314.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390314.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

1583. 統計不開心的朋友

1583. 統計不開心的朋友 給你一份 n 位朋友的親近程度列表,其中 n 總是 偶數 。 對每位朋友 i,preferences[i] 包含一份 按親近程度從高到低排列 的朋友列表。換句話說,排在列表前面的朋友與 i 的親近程度比排在列表后面的朋友更高。每個列…

uva 247(floyd傳遞閉包)

為什么&#xff0c;逗號后面&#xff0c;還有空格........ #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> #include <map> using namespace std; const int maxn50; int d[maxn][max…

VS Code 的常用快捷鍵和插件

注:文章摘自 風行天下一萬號 - 博客園 vs code 的常用快捷鍵 1、注釋&#xff1a; 單行注釋&#xff1a;[ctrlk,ctrlc] 或 ctrl/取消單行注釋&#xff1a;[ctrlk,ctrlu] (按下ctrl不放&#xff0c;再按k u)多行注釋&#xff1a;[altshiftA]多行注釋&#xff1a;/**2、移動行&a…

python包numpy_NumPy Python科學計算軟件包的終極指南

python包numpyNumPy (pronounced "numb pie") is one of the most important packages to grasp when you’re starting to learn Python.NumPy(讀作“麻木派”)是您開始學習Python時要掌握的最重要的軟件包之一。 The package is known for a very useful data str…

NGINX原理 之 SLAB分配機制(轉)

1 引言 眾所周知&#xff0c;操作系統使用伙伴系統管理內存&#xff0c;不僅會造成大量的內存碎片&#xff0c;同時處理效率也較低下。SLAB是一種內存管理機制&#xff0c;其擁有較高的處理效率&#xff0c;同時也有效的避免內存碎片的產生&#xff0c;其核心思想是預分配。其按…

apk之間數據共享的方式

1、四大組件之ContentProvider大法2、shareUserId3、apk均去遠端獲取配置文件&#xff08;或接口&#xff09;4、AIDL&#xff08;bindService&#xff09;5、SharePreference設置為MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE模式&#xff0c;由于存在安全問題&#xff0c;已被…

藍橋杯java 基礎練習 十六進制轉十進制

問題描述從鍵盤輸入一個不超過8位的正的十六進制數字符串&#xff0c;將它轉換為正的十進制數后輸出。注&#xff1a;十六進制數中的10~15分別用大寫的英文字母A、B、C、D、E、F表示。樣例輸入FFFF樣例輸出65535import java.math.BigInteger; import java.util.Scanner;public …

dynamic web module消失不見

2019獨角獸企業重金招聘Python工程師標準>>> 方法1&#xff1a;在project Facets選項中勾選Dynamic Web Module即可 方法2&#xff1a; 我用eclipse對項目進行修改名稱&#xff0c;修改成功后。項目就沒有Deployment Descriptor&#xff08;如下圖紅色框中&#xff…

576. 出界的路徑數

576. 出界的路徑數 給你一個大小為 m x n 的網格和一個球。球的起始坐標為 [startRow, startColumn] 。你可以將球移到在四個方向上相鄰的單元格內&#xff08;可以穿過網格邊界到達網格之外&#xff09;。你 最多 可以移動 maxMove 次球。 給你五個整數 m、n、maxMove、star…

telnet命令發送郵件

下面的例子是用qq的smtp服務器。 set localecho 本地回顯啟用 telnet smtp.qq.com 25 220 smtp.qq.com Esmtp QQ Mail Server helo sis 250 smtp.qq.com//服務器返回250 smtp.qq.com STARTTLS 220 Ready to start TLS//服務器返回 220 準備開啟TLS通訊 auth login 334 VXNlcm5h…

myelcipse和maven搭建項目

偷懶一下&#xff0c;完了補充 轉載&#xff1a;https://www.cnblogs.com/jr1260/p/6438811.html https://www.cnblogs.com/yangmingyu/p/6908519.html https://www.cnblogs.com/henuyuxiang/p/6288476.html 轉載于:https://www.cnblogs.com/0914lx/p/8342343.html

551. 學生出勤記錄

551. 學生出勤記錄 I 給你一個字符串 s 表示一個學生的出勤記錄&#xff0c;其中的每個字符用來標記當天的出勤情況&#xff08;缺勤、遲到、到場&#xff09;。記錄中只含下面三種字符&#xff1a; ‘A’&#xff1a;Absent&#xff0c;缺勤 ‘L’&#xff1a;Late&#xff…

JavaScript實現職責鏈模式

什么是職責鏈模式 職責鏈模式的定義是&#xff1a;使多個對象都有機會處理請求&#xff0c;從而避免請求的發送者和接收者之間的耦合關系&#xff0c;將這些對象連成一條鏈&#xff0c;并沿著這條鏈傳遞該請求&#xff0c;直到有一個對象處理它為止。舉個例子&#xff1a;當你從…

Metrics介紹和Spring的集成

參考&#xff1a; http://colobu.com/2014/08/08/Metrics-and-Spring-Integration/ https://www.cnblogs.com/yangecnu/p/Using-Metrics-to-Profiling-WebService-Performance.html

配置 aws cli_AWS CLI教程–如何安裝,配置和使用AWS CLI了解您的資源環境

配置 aws cliHow to get exactly the account and environment information you need to manage your AWS account using just the AWS CLI如何僅使用AWS CLI準確獲取管理AWS賬戶所需的賬戶和環境信息 Installing the AWS CLI is actually quite simple. The best way to get …

grep遞歸查找頭文件_Grep命令教程–如何使用遞歸查找在Linux和Unix中搜索文件

grep遞歸查找頭文件grep stands for Globally Search For Regular Expression and Print out. It is a command line tool used in UNIX and Linux systems to search a specified pattern in a file or group of files. grep代表全局搜索正則表達式并打印出來 。 它是UNIX和Li…

C++ 前置聲明

&#xff08;一&#xff09;class的前置聲明 class的前置聲明有兩種。 pre.hclass PreA {}; main.hclass PreA; class Main {};//或者 class Main {class PreA* A; }; (二) struct前置聲明 struct的前置聲明只能用第一種。 &#xff08;三&#xff09; 有typedef的前置聲明 Pr…

2.18 特殊權限set_uid 2.19 特殊權限set_gid 2.20 特殊權限stick_bit 2.21 軟鏈接文件 2.22 硬連接文件...

2019獨角獸企業重金招聘Python工程師標準>>> 特殊權限set_uid set_uid:該權限針對二進制可執行文件&#xff0c;使文件在執行階段具有文件所有者的權限&#xff1b; 通俗一點講就是&#xff0c;普通用戶想要訪問一個沒有其他用戶可執行權限的目錄時&#xff0c;暫時…

345. 反轉字符串中的元音字母

345. 反轉字符串中的元音字母 給你一個字符串 s &#xff0c;僅反轉字符串中的所有元音字母&#xff0c;并返回結果字符串。 元音字母包括 ‘a’、‘e’、‘i’、‘o’、‘u’&#xff0c;且可能以大小寫兩種形式出現。 示例 1&#xff1a; 輸入&#xff1a;s “hello” 輸…

通過制作數字桌面游戲和Web應用程序學習JavaScript

Building 2D games can be a great way to learn JavaScript, especially when working through the basics of complex tabletop game logic.制作2D游戲可能是學習JavaScript的好方法&#xff0c;尤其是在研究復雜的桌面游戲邏輯基礎時。 In this series, I’m going to intr…