Skip to content

Instantly share code, notes, and snippets.

@zfogg
Created December 21, 2013 07:55
Show Gist options
  • Save zfogg/8066693 to your computer and use it in GitHub Desktop.
Save zfogg/8066693 to your computer and use it in GitHub Desktop.
Fibonacci in Go
package main
import "fmt"
import "math/big"
func main() {
f := fibonacci()
for i := 0; i < 100; i++ {
fmt.Println(f(i))
}
}
func fibonacci() (func(int) *big.Int) {
s := map[int]*big.Int { 0:big.NewInt(1), 1:big.NewInt(1) }
var f func(int) (*big.Int)
f = func(n int) (*big.Int) {
if v,ok := s[n]; ok { return v }
s[n] = big.NewInt(0).Add(f(n-2), f(n-1))
return s[n]
}
return func(i int) *big.Int {
return f(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment