Skip to content

Instantly share code, notes, and snippets.

@KaeruCT
Created September 8, 2013 21:53
Show Gist options
  • Save KaeruCT/6488745 to your computer and use it in GitHub Desktop.
Save KaeruCT/6488745 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/big"
)
func fibonacciCalculator() func(n int) *big.Int{
cache := map[int]*big.Int{
0: big.NewInt(0),
1: big.NewInt(1),
}
var fib func(n int) *big.Int;
fib = func(n int) *big.Int{
if result, ok := cache[n]; ok {
return result
}
cache[n] = big.NewInt(0).Add(fib(n-1), fib(n-2))
return cache[n]
}
return fib
}
func main() {
fib := fibonacciCalculator()
for i := 0; i <= 99; i++ {
fmt.Println("fib(", i, ") = ", fib(i).String())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment