Is the algorithm below the best way of generating the Fibonacci series? Or is there a better approach?
This is my C program to print fibonacci series.
Adding to @RogerFernandezGuri's excellent answer, a possibly more readable answer in Go for printing a Fibonacci series.
package main
import "fmt"
func main() {
a, b := 0, 1
h := func() int {
a, b = b, a+b
return b-a
}
fibonacci := func() func() int { return h }
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f()) //0, 1, 1, 2, 3, 5, 8, 13, 21, 34
}
}