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
+6k Fontello : How to load and use fonts?
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+28.1k Golang : Move file to another directory
+10.3k Golang : Random Rune generator
+7.6k Golang : Dealing with struct's private part
+13.2k Golang : Handle or parse date string with Z suffix(RFC3339) example
+11.2k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+6.6k Unix/Linux : How to get own IP address ?
+8.2k Android Studio : Rating bar example
+17.5k Golang : Linked list example
+7.4k Linux : How to fix Brother HL-1110 printing blank page problem