Skip to content

Instantly share code, notes, and snippets.

@parasyte
Created November 15, 2012 09:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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())
}
}
@arrterian
Copy link

My implementation

func fibonacci() func() int {
	var prewFibo, currentFibo int;
	
	return func() int {
		if currentFibo == 0 {
			defer func() { currentFibo = 1 }()
		}
		
		defer func() {
			prewFibo, currentFibo = currentFibo, currentFibo + prewFibo
		}()
		
		return currentFibo
	}
}

@ssambros
Copy link

ssambros commented Jan 6, 2017

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

@ssambros
Copy link

ssambros commented Jan 6, 2017

this anonymous function is even better

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

@cahitbeyaz
Copy link

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	
	first:=0
	second:=1
	var myFn =func() int{
		result:=first
		temp:=first
		first=second
		second=temp+second
		
		
		return result;
	}
	return myFn
}

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

@danielrangelmoreira
Copy link

package main

func fib(n int) int {
	fn := make(map[int]int)
	for i := 0; i <= n; i++ {
		var f int
		if i <= 2 {
			f = 1
		} else {
			f = fn[i-1] + fn[i-2]
		}
		fn[i] = f
	}
	return fn[n]
}

func main() {
	var x = fib(100)
	println(x)
}

@danielrangelmoreira
Copy link

Linear O(n) implementation as seen in Youtube Video Lecture

3736710778780434371
./fibonacci 10 1000 10000 20000
0,00s user 0,00s system 48% cpu 0,009 total

package main

import (
	"fmt"
	"math/big"
	"os"
	"strconv"
)

func fib(n int) *big.Int {
	fn := make(map[int]*big.Int)

	for i := 0; i <= n; i++ {
		var f = big.NewInt(0)
		if i <= 2 {
			f.SetUint64(1)
		} else {
			f = f.Add(fn[i-1], fn[i-2])
		}
		fn[i] = f
	}
	return fn[n]
}

func main() {
	for _, s := range os.Args[1:] {
		n, e := strconv.Atoi(s)
		if e != nil {
			panic(e)
		}
		fmt.Printf("%s\n", fib(n))
	}

}

@SaturnusK1
Copy link

SaturnusK1 commented Jul 22, 2017

My first implementation was like this because I had a background very different than Go. 😑

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	fib1 := 0
	fib2 := 1
	return func() int {
		fib := fib1 + fib2
		fib1 = fib2
		fib2 = fib
		return fib1
	}
}

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

@yogesh-desai
Copy link

My Implementation:
The first number must be '0' in Fibonacci.

package main

import "fmt"

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

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

@SorokinaT
Copy link

Hi, guys. I just started to learn Golang, and I can't understand this summing
x, y = y, x + y
Can u help me with it?

@longth9
Copy link

longth9 commented Feb 19, 2018

@SorokinaT It's multiple assignments. Take a look at this stackoverflow thread.

@s0kil
Copy link

s0kil commented Apr 13, 2018

The Fibonacci sequence is defined as: fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2).

@tiagomestre
Copy link

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 {
		result := x
		x, y = y, x+y
		return result
	}
}

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

@damiandragowski
Copy link

damiandragowski commented Apr 25, 2018

Here is example with array using :)

package main

package main

import "fmt"

func fibonacci() func() int {
	temp := [2] int { 0, 0 }
	return func() int {
		if temp[1] == 0  {
			temp[1] = 1
			return 0
		}
		temp[0], temp[1] = temp[1], temp[0]+temp[1]
		return temp[0]
	}
}

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

@hjdesai2
Copy link

hjdesai2 commented May 8, 2018

Pretty Simple

package main

import "fmt"

func fibo(n int) int {
	x, y := 0, 1
	for i := 0; i < n; i++ {
		x, y = y, x+y
	}
	return x
}

func main() {
	var input int
	fmt.Print("Enter the Number:")
	fmt.Scanf("%d", &input)
	fmt.Println("Result:", fibo(input))
}

@stoimen01
Copy link

Here is my shot starting from 0 :

package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	tmp1, tmp2 := 0, 0
	return func() int {
		tmp1, tmp2 = tmp2, tmp1 + tmp2
		if tmp2 == 0 { tmp1 = 1 }
		return tmp2
	}
}

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

@suifengtec
Copy link

suifengtec commented May 14, 2018

goroutinue is useful !

/*


Windows

go build -o f1.exe f1.go && f1

or

go build -o f1.exe f1.go

f1 18


Linux

go build -o f1 f1.go && ./f1

or

go build -o f1 f1.go

./f1 18


*/
package main

import (
	"fmt"
	"os"
	"strconv"
)

var length int

func Fibonacci(count int) {
	//只写通道,用于传递数列上的数据项
	ch := make(chan int)
	//只读通道, 用于标记程序是否结束
	shouldQuit := make(chan bool)

	//不停的读取 ch 的内容,直到读完给定长度 count 个数据.
	go func() {

		for i := 0; i < count; i++ {
			n := <-ch
			fmt.Printf("%d, ", n)
		}

		shouldQuit <- true

	}()

	//第1项,第2项都为1
	x, y := 0, 1

	//开始无限循环
	for {

		//监听数据流动
		select {
		//数据通道可写入时,把x传递进去,并执行数列规则
		case ch <- x:
			//数列规则是:后一项是前两项的和
			x, y = y, x+y
		case isDone := <-shouldQuit:
			fmt.Println("\nisDoen?", isDone)
			return
		}
	}

}

func GetLength() {

	n := 12

	if len(os.Args) == 2 {
		n, _ = strconv.Atoi(os.Args[1])
	}

	length = n

}

func main() {

	GetLength()
	// 获取该数列上的多少个数字
	Fibonacci(length)

}

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