Golang : Fibonacci number generator examples
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence.
1,1,2,3,5,8,13,21,34,55,89,144...
Here are some Fibonacci numbers generators examples.
Example 1 :
package main
import "fmt"
func main() {
for i, j := 0, 1; j < 100; i, j = i+j,i {
fmt.Println(i)
}
}
Example 2:
package main
import (
"fmt"
)
func fibonacci() chan int {
in := make(chan int, 2)
in <- 0
in <- 1
out := make(chan int)
go func(in, out chan int) {
for {
a := <-in
in <- <-in + a
in <- a
out <- a
}
}(in, out)
return out
}
func main() {
x := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(<-x)
}
}
References :
http://en.wikipedia.org/wiki/Fibonacci_number
https://github.com/Sinute/go-daily/blob/master/chan-fibonacci.go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.9k Golang : Combine slices but preserve order example
+5.8k Golang : How to check if a string with spaces in between is numeric?
+2.8k Chrome : How to block socketloop.com links in Google SERP?
+4.6k Golang : Example of custom handler for Gorilla's Path usage.
+5k Golang : Not able to grep log.Println() output
+6.8k Golang : Convert octal value to string to deal with leading zero problem
+18.2k Golang : Set and Get HTTP request headers example
+6k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+8.6k Golang : Clean formatting/indenting or pretty print JSON result
+7.8k Golang : GTK Input dialog box examples
+3.8k Unix/Linux : Get reboot history or check when was the last reboot date
+6.8k Golang : Check if user agent is a robot or crawler example