Created
December 2, 2018 17:07
-
-
Save srleyva/cdb48ffb9031b158371ab3ee6e066235 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" | |
"sort" | |
) | |
func main() { | |
cache := make(map[int]int) | |
for i := 1; i < 50; i++ { | |
fib(i, cache) | |
} | |
keys := []int{} | |
for k := range cache { | |
keys = append(keys, k) | |
} | |
sort.Ints(keys) | |
for k := range keys { | |
fmt.Printf("%d:%d\n", k, cache[k]) | |
} | |
} | |
func fib(n int, cache map[int]int) int { | |
if n < 2 { | |
return n | |
} | |
if cache[n] == 0 { | |
cache[n] = fib(n-1, cache) + fib(n-2, cache) | |
} | |
return cache[n] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment