Go Fibonacci number

package main
import (
	"fmt"
)

func fbn(n int) ([]uint64) {
	// declare a slice, slice size n
	fbnSlice := make([]uint64, n)
	// first Fibonacci number and the second number is 1
	fbnSlice[0] = 1
	fbnSlice[1] = 1
	// loop performed for the number of columns to hold Fibonacci
	for i := 2; i < n; i++ {
		fbnSlice [i] = fbnSlice [i - 1] + fbnSlice [i - 2]
	}

	return fbnSlice
}

func main() {

	/*
	1) may receive a n int
	2) can be Fibonacci series into slices
	3) suggest that the number of columns in the form of Fibonacci:
	arr [0] = 1; arr [1] = 1; arr [2] = 2; arr [3] = 3; arr [4] = 5; arr [5] = 8

	Thinking
	1. Declare a function fbn (n int) ([] uint64)
	2. Programming fbn (n int) loop to be stored for the Fibonacci series 0 = "1 1 =" 1
	*/

	// a test to see if it works
	fnbSlice := fbn(20)
	fmt.Println("fnbSlice=", fnbSlice)
	//fnbSlice= [1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765]

}

  

Guess you like

Origin www.cnblogs.com/yzg-14/p/12229988.html