The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1. [1]

## Method 1 ( Use recursion )

Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package main import ( "fmt" ) func fibonacci(n int) int { if n <= 1 { return n } return fibonacci(n-1) + fibonacci(n-2) } func main() { fmt.Println("Fibonacci Using Recursion") fmt.Println(fibonacci(6)) } |

### output*:-*

Fibonacci Using Recursion

8

## Method 2 ( Use Dynamic Programming )

Time Complexity: O(n)

Extra Space: O(n)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package main import ( "fmt" ) func fibonacci(n int) []int { f := make([]int, n+1) var i int /* 0th and 1st number of the series are 0 and 1*/ f[0] = 0 f[1] = 1 for i = 2; i <= n; i++ { /* Add the previous 2 numbers in the series and store it */ f[i] = f[i-1] + f[i-2] } return f } func main() { fmt.Println("Fibonacci Using Dynamic Programming") fmt.Println(fibonacci(6)) } |

### output:-

Fibonacci Using Dynamic Programming

[0 1 1 2 3 5 8]

## Method 3 ( Fibonacci Using Dynamic Programming and Space Optimization )

We can optimize the space used in method 2 by storing the previous two numbers only because that is all we need to get the next Fibonacci number in series.

Time Complexity: O(n)

Extra Space: O(1)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package main import ( "fmt" ) func fibonacci(n int, c chan int) { a, b := 0, 1 for i := 0; i < n; i++ { c <- a a, b = b, a+b } close(c) } func main() { fmt.Println("Fibonacci Using Dynamic Programming and Space Optimizion") c := make(chan int, 10) go fibonacci(cap(c), c) for i := range c { fmt.Printf("%v ", i) } } |

### output:-

Fibonacci Using Dynamic Programming and Space Optimizion

0 1 1 2 3 5 8 13 21 34

## Method 4 ( Using power of the matrix {{1,1},{1,0}} )

This is another O(n) which relies on the fact that if we multiply the matrix M = {{1,1},{1,0}}, n times to itself, (in other words calculate power(M, n )), then we get the (n+1)th Fibonacci number as the element at row and column (0, 0) in the resultant matrix.

Time Complexity: O(n)

Extra Space: O(1)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package main import ( "fmt" ) func fibonacci(n int) int { F := [][]int{} // These are the first two rows. row1 := []int{1, 1} row2 := []int{1, 0} // Append each row to the two-dimensional slice. F = append(F, row1) F = append(F, row2) if n == 0 { return 0 } power(F, n-1) return F[0][0] } /* Helper function that calculates F[][] raise to the power n and puts the result in F[][] Note that this function is designed only for fibonacci() and won't work as general power function */ func power(F [][]int, n int) { M := [][]int{} // These are the first two rows. row1 := []int{1, 1} row2 := []int{1, 0} // Append each row to the two-dimensional slice. M = append(M, row1) M = append(M, row2) // n - 1 times multiply the matrix to {{1,0},{0,1}} for i := 2; i <= n; i++ { multiply(F, M) } } /* function that multiplies 2 matrices F1 and F2 of size 2*2, and puts the multiplication result back to F[][] */ func multiply(F1 [][]int, F2 [][]int) { x := F1[0][0]*F2[0][0] + F1[0][1]*F2[1][0] y := F1[0][0]*F2[0][1] + F1[0][1]*F2[1][1] z := F1[1][0]*F2[0][0] + F1[1][1]*F2[1][0] w := F1[1][0]*F2[0][1] + F1[1][1]*F2[1][1] F1[0][0] = x F1[0][1] = y F1[1][0] = z F1[1][1] = w } func main() { fmt.Println("Using power of the matrix {{1,1},{1,0}}") fmt.Println(fibonacci(10)) } |

### output:-

Using power of the matrix {{1,1},{1,0}}

55

## Method 5 ( Optimized Method 4 )

The method 4 can be optimized to work in O(Logn) time complexity. We can do recursive multiplication to get power(M, n) in the previous method

(Similar to the optimization done in this post).

Time Complexity: O(Logn)

Extra Space: O(Logn) if we consider the function call stack size, otherwise O(1)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package main import ( "fmt" ) func fibonacci(n int) int { F := [][]int{} // These are the first two rows. row1 := []int{1, 1} row2 := []int{1, 0} // Append each row to the two-dimensional slice. F = append(F, row1) F = append(F, row2) if n == 0 { return 0 } power(F, n-1) return F[0][0] } /* Helper function that calculates F[][] raise to the power n and puts the result in F[][] Note that this function is designed only for fibonacci() and won't work as general power function */ func power(F [][]int, n int) { if n == 0 || n == 1 { return } M := [][]int{} // These are the first two rows. row1 := []int{1, 1} row2 := []int{1, 0} // Append each row to the two-dimensional slice. M = append(M, row1) M = append(M, row2) power(F, n/2) multiply(F, F) if n%2 != 0 { multiply(F, M) } } /* Helper function that multiplies 2 matrices F and M of size 2*2, and puts the multiplication result back to F[][] */ func multiply(F [][]int, M [][]int) { x := F[0][0]*M[0][0] + F[0][1]*M[1][0] y := F[0][0]*M[0][1] + F[0][1]*M[1][1] z := F[1][0]*M[0][0] + F[1][1]*M[1][0] w := F[1][0]*M[0][1] + F[1][1]*M[1][1] F[0][0] = x F[0][1] = y F[1][0] = z F[1][1] = w } func main() { fmt.Println("Using power of the matrix {{1,1},{1,0}}") fmt.Println(fibonacci(9)) } |

### output:-

Using power of the matrix {{1,1},{1,0}}

34

## Method 6 (O(Log n) Time)

Below is one more interesting recurrence formula that can be used to find the n’th Fibonacci Number in O(Log n) time.

If n is even then k = n/2:

F(n) = [2*F(k-1) + F(k)]*F(k)

If n is odd then k = (n + 1)/2

F(n) = F(k)*F(k) + F(k-1)*F(k-1)

Time complexity of this solution is O(Log n) as we divide the problem to half in every recursive call.

### Related Posts

- 10 Steps to Build a Time Series Model
As promised in our previous blog “Introduction to Time Series Forecasting in R,” we are back with our next installment to build a time series model. Time series modeling is…

- Understanding Teradata Wallet
Teradata Wallet is a facility for storage of sensitive/secret information, such as Teradata Database user passwords. Users are able to save and retrieve items by using this facility. Teradata wallet…

- Introduction to Messaging
Messaging is one of the most important aspects of modern programming techniques. Majority of today's systems consist of several modules and external dependencies. If they weren't able to communicate with…

- AI in Banking(2) – Chatbots
Hello readers, as promised, we are here with our 2nd blog in the series of blogs on “AI in Banking and Financial Services”, focusing on Chatbots. As discussed in the…

- Perl One-Liners
What are Perl One-Liners? As the name suggests, Perl One-Liners are small single line of codes that can perform several tasks, ranging from changing line spacing and numbering lines to…

- Show/Hide Filter in Tableau
Despite the presence of several quick filters, legends, and other links, we may sometimes require more space to display data in the Tableau Dashboard. There’s, however, a restriction on the…