Is there a better code for printing fibonacci series?

后端 未结 3 999
粉色の甜心
粉色の甜心 2020-12-12 08:14

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.



        
3条回答
  •  没有蜡笔的小新
    2020-12-12 08:25

    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
        }
    }
    

提交回复
热议问题