Solution Review: Finding Fibonacci Numbers with Array
This lesson discusses the solution to the challenge given in the previous lesson.
We'll cover the following...
We'll cover the following...
Go (1.6.2)
package mainimport "fmt"var fib [10]int64 // global array containing Fibonacci valuesfunc fibs() [10]int64{fib[0] = 1 // base cases for 0fib[1] = 1 // base case for 1for i:= 2; i <10; i++ {fib[i] = fib[i-1] + fib[i-2] // recursive case without recursion using array}return fib}func main() {arr := fibs()for i:=0; i < 10; i++ {fmt.Printf("The %d-th Fibonacci number is: %d\n", i, arr[i])}}
In the code above, at line 4, we create a global variable of type integer array called fib
with length 10. This array will contain the first 10 Fibonacci numbers. Now, look at the header ...