Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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())
}
}
@danmux

This comment has been minimized.

Copy link

danmux commented Apr 30, 2013

Fibonacci should have zero as its first value...

func fibonacci() func() int {
    x, y := 0, 1
    return func() (r int) {
        r = x
        x, y = y, x + y
        return 
    }
}
@arrterian

This comment has been minimized.

Copy link

arrterian commented Nov 26, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

sergeysambros commented Jan 6, 2017

this anonymous function is even better

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

@cahitbeyaz

This comment has been minimized.

Copy link

cahitbeyaz commented Mar 27, 2017

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

This comment has been minimized.

Copy link

danielrangelmoreira commented Apr 18, 2017

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

This comment has been minimized.

Copy link

danielrangelmoreira commented Apr 18, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

yogesh-desai commented Oct 13, 2017

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

This comment has been minimized.

Copy link

SorokinaT commented Feb 18, 2018

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

This comment has been minimized.

Copy link

longth9 commented Feb 19, 2018

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

@s0kil

This comment has been minimized.

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

This comment has been minimized.

Copy link

tiagomestre commented Apr 24, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

stoimen01 commented May 13, 2018

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

This comment has been minimized.

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)

}
@yasserlamsaougar

This comment has been minimized.

Copy link

yasserlamsaougar 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())
	}
}
@Chen-Su

This comment has been minimized.

Copy link

Chen-Su commented Feb 12, 2019

@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

This comment has been minimized.

Copy link

fivejjs commented Mar 6, 2019

this anonymous function is even better

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

This looks good. 👍

@PurnaRaminiPersonal

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner Author

parasyte commented Jul 24, 2019

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

This comment has been minimized.

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⏎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.