Solution Review: Finding Fibonacci Numbers with Slices
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport "fmt"func main() {result := fibarray(5)for ix, fib := range result {fmt.Printf("The %d-th Fibonacci number is: %d\n", ix, fib)}}func fibarray(term int) []int { // calculating Fibonacci for first term numbersfarr := make([]int, term)//base caseif term == 1 {farr[0] = 0return farr}farr[0], farr[1] = 0, 1// recursive casefor i:= 2; i < term; i++ {farr[i] = farr[i-1] + farr[i-2] // calculating sequence for i index}return farr}
Get hands-on with 1400+ tech skills courses.