Created
September 8, 2013 21:53
-
-
Save KaeruCT/6488745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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