Skip to content

Instantly share code, notes, and snippets.

@parasyte
Created November 15, 2012 09:44
Show Gist options
  • Save parasyte/4077694 to your computer and use it in GitHub Desktop.
Save parasyte/4077694 to your computer and use it in GitHub Desktop.
Fibonacci closure: http://tour.golang.org/#48
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() int {
x, y = y, x + y
return x
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Copy link

ghost commented Sep 8, 2018

No need for conditions

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int64 {
	var nm2 int64 = -1
	var res int64 = 1
	return func() int64 {
		res, nm2 = res+nm2, res
		return res
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 100; i++ {
		fmt.Println(f())
	}
}

@allensu0314
Copy link

@RomanValihura: Here is another way with defer.

func fibonacci() func() int {
	a, b := 0, 1
	return func() int {
		defer func() {
			a = a + b
			a, b = b, a
		}()
		return a
	}
}

This solution is elegant.

@fivejjs
Copy link

fivejjs commented Mar 6, 2019

this anonymous function is even better

defer func() {
    a, b = b, a + b
}()

This looks good. 👍

@PurnaRaminiPersonal
Copy link

PurnaRaminiPersonal commented Jul 23, 2019

Following code generates reverse fibonacci series (Enhanced code from this blog) - Purna Ramini

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() (r int) {
r = x
x, y = y, x + y
return
}
}

func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
defer fmt.Println(f())
}
}

@parasyte
Copy link
Author

This gist is 7 years going strong!

FWIW, the best solution for computing Nth-Fib is the closed-form expression. See: https://gist.github.com/parasyte/cef9b3552d837f7a1e14b0be29918437

@pqant
Copy link

pqant commented Aug 24, 2019

package main

import (
	"fmt"
	"log"
	"time"
)

func fibByChannel(val int64) <-chan int64 {
	result := make(chan int64)
	go func() {
		defer close(result)
		if val < 2 {
			result <- val
			return
		}
		result <- <-fibByChannel(val - 1) + <-fibByChannel(val - 2)
	}()
	return result
}

func fibByNormal(num int) int {
	if num < 2 {
		return num
	}
	return fibByNormal(num-2)+fibByNormal(num-1)
}

func main() {
	defer fmt.Printf("\nDONE")

	timeStart := time.Now()
	log.Printf("***** %v *****","Begin of fibByChannel")
	log.Printf("%v\n", <-fibByChannel(10))
	log.Printf("***** %v *****   - [%v]","End of fibByChannel",time.Since(timeStart).Seconds())

	timeStart = time.Now()
	log.Printf("***** %v *****","Begin of fibByNormal")
	log.Printf("%v\n", fibByNormal(10))
	log.Printf("***** %v *****   - [%v]","End of fibByNormal",time.Since(timeStart).Seconds())
}

/* *********************************************************************************************** */

//2019/08/25 01:18:38 ***** Begin of fibByChannel *****
//2019/08/25 01:18:38 55
//2019/08/25 01:18:38 ***** End of fibByChannel ***** - [0.00045481]
//2019/08/25 01:18:38 ***** Begin of fibByNormal *****
//2019/08/25 01:18:38 55
//2019/08/25 01:18:38 ***** End of fibByNormal ***** - [4.158e-06]

//DONE⏎

@shishirmdr
Copy link

Very late but here's my solution. This also makes sure that the pattern starts from 0.

func fibonacci() func() int {
    a, b, c := 0, 1, 0

    return func() int {
        c += a
        a = b
        b = c
  
        return c
    }
}

@vjykrthk
Copy link

vjykrthk commented May 3, 2020

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	result := 0
	initial := 1
	return func() int {
		temp := result
		result += initial
		initial = temp
		return initial
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

@AlexandrHeroCoud
Copy link

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	i := 0
	iOne := int(1)
	ret := 0
	return func() int {
		if i == 0{
			i = 1
		}
		iOne = ret
		ret = i + iOne
		i = iOne

		return ret
	}
}

func main() {
	f := fibonacci()
	for i := 0; true; i++ {
		fmt.Println(f())
	}
}

@ngocdiep
Copy link

ngocdiep commented Jul 6, 2020

func fibonacci() func() int {
x, y := -1, 0
return func() int {
x, y = y, x + y
return (-1) * x
}
}

@khayru11oh
Copy link

package main
import "fmt"
func main() {
a := 0
for i:=1; i<=1000; {
fmt.Println(i)
a, i = i, i+a
}
}

@raihankhan
Copy link

func fibonacci() func() int {
a := 0
b := 1
return func() int {
defer func() {
fib := a+b
a = b
b = fib
}()
return a
}
}

func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

@sina-devel
Copy link

func Fibonacci() func() uint {
	var a, b uint = 1, 0
	return func() uint {
		a, b = b, a+b
		return a
	}
}

@msh2050
Copy link

msh2050 commented Jan 27, 2022

thanks for the gist,it is very much useful
I make Benchmarking for deferent Fibonacci functions and algorithms with running unit test
https://github.com/msh2050/GoFibonacciBench

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment