Swift 語言顯示斐波那契數列的程式


斐波那契數列是一個數列,其中每個數都是前兩個數之和,該數列的起始數字為 0 和 1。因此,一般的斐波那契數列為:

0, 1, 1, 2, 3, 5, 8, 13, 21, ......

公式

以下是斐波那契數列的公式:

Fn = Fn-1 + Fn-2

下面是一個演示:

假設我們輸入以下內容:

Number = 5

以下是期望的輸出:

Fibonacci Series is- 0, 1, 1, 2, 3

我們可以使用以下任何方法找到斐波那契數列:

  • 迭代法

  • 遞迴法

迭代法

我們可以使用迭代法建立斐波那契數列。在這種方法中,我們使用迴圈來查詢數列。

演算法

演算法解釋如下:

  • 步驟 1 - 建立一個函式。

  • 步驟 2 - 宣告三個變數,值分別為 - n1 = 0,n2 = 1,nR = 0。

  • 步驟 3 - 執行一個從 0 到 <num 的 for 迴圈,透過新增前面的數字來查詢斐波那契數列。

  • 步驟 4 - 宣告一個值為 - val = 10 的變數

  • 步驟 5 - 使用一個引數呼叫函式並顯示最終輸出。

示例

以下程式演示瞭如何使用迭代法計算整數中數字的個數。

import Foundation import Glibc func fibonacciSeries(num: Int) -> Int{ // The value of 0th and 1st number of the fibonacci series are 0 and 1 var n1 = 0 var n2 = 1 // To store the result var nR = 0 // Adding two previous numbers to find ith number of the series for _ in 0..<num{ nR = n1 n1 = n2 n2 = nR + n2 } return n1 } var val = 10 print("Following is the Fibonacci series:") // Iterate for every number stating from 0 to val for j in 0...val{ let output = fibonacciSeries(num: j) print(output) }

輸出

Following is the Fibonacci series:
0
1
1
2
3
5
8
13
21
34

在上面的程式碼中,我們建立了一個名為 fibonacciSeries() 的函式,它接受一個引數並返回斐波那契數列。此函式使用 for 迴圈計算前兩個數字的和以找到下一個數字,使用以下程式碼:

for _ in 0..<num{
   nR = n1 
   n1 = n2
   n2 = nR + n2
}

在函式外部,我們建立了一個名為 val = 10 的變數。現在我們使用 for 迴圈顯示斐波那契數列。此迴圈從 0 執行到 10,在此迴圈內,我們使用一個引數呼叫 fibonacciSeries() 函式,這意味著我們呼叫 fibonacciSeries() 函式 10 次以查詢從 0 到 10 的數列。

遞迴法

我們可以使用遞迴法建立斐波那契數列。在這種方法中,我們建立一個遞迴函式,該函式呼叫自身以查詢從 1 到指定數字的斐波那契數列。

演算法

演算法解釋如下:

  • 步驟 1 - 建立一個函式。

  • 步驟 2 - 使用 if-else 語句檢查給定數字是否為 1 或 0,如果給定數字為 1,則返回 1。如果給定數字為 0,則返回 0。

  • 步驟 3 - 透過呼叫函式本身返回斐波那契數列。

    return fibonacciSeries(num: num - 1) + fibonacciSeries(num: num - 2)

  • 步驟 4 - 宣告一個值為 - val = 10 的變數

  • 步驟 5 - 執行一個從 0 到 val 的 for 迴圈

  • 步驟 6 - 使用一個引數呼叫函式並顯示最終輸出。

示例

以下程式演示瞭如何使用遞迴法計算整數中數字的個數。

import Foundation import Glibc func fibonacciSeries(num: Int) -> Int{ // Checking number for num 0 and 1 // Or we can say it is the base condition for recursive // function if (num == 0){ return 0 } else if (num == 1){ return 1 } // To find the with number we add the previous two numbers // Here we find the previous two numbers by calling the function itself and return the result. return fibonacciSeries(num: num - 1) + fibonacciSeries(num: num - 2) } var val = 10 print("Following is the Fibonacci series:") // Iterate for every number stating from 0 to val for j in 0...val{ let output = fibonacciSeries(num: j) print(output) }

輸出

Following is the Fibonacci series:
0
1
1
2
3
5
8
13
21
34
55

在這裡,在上面的程式碼中,我們建立了一個名為 fibonacciSeries() 的遞迴函式。它接受一個引數並返回斐波那契數列。現在在函式外部,我們建立了一個名為 val = 10 的變數。為了顯示從 0 到 10 的斐波那契數列,我們使用一個 for 迴圈,此迴圈從 0 執行到 10,在此迴圈內,我們使用一個引數呼叫 fibonacciSeries() 函式,此函式返回從 0 到 10 的斐波那契數列。因此,fibonacciSeries() 函式的工作原理是:

1st call of fibonacciSeries() with 0: fibonacciSeries(0) = return 0
2nd call of fibonacciSeries() with 1: fibonacciSeries(1) = return 1
3rd call of fibonacciSeries() with 3: fibonacciSeries(2) =
return fibonacciSeries(num: 2 - 1) + fibonacciSeries(num: 2 -  2) = fibonacciSeries(num:1) + fibonacciSeries(num: 0) = 1 + 0 = 1
return 1
4th call of fibonacciSeries() with 4: fibonacciSeries(4) =
return fibonacciSeries(num: 4 - 1) + fibonacciSeries(num: 4 -  2) = fibonacciSeries(num:3) + fibonacciSeries(num: 2) = 1 + 1 = 2
return 2
...
10th call of fibonacciSeries() with 10: fibonacciSeries(10) =
return fibonacciSeries(num: 10 - 1) + fibonacciSeries(num: 10 -  2) = fibonacciSeries(num:9) + fibonacciSeries(num: 8) = 21 + 34 = 55
return 55

更新於:2022年8月5日

6000+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告