Last active
May 17, 2023 03:06
-
-
Save caelifer/68d253f92386b856d2521094ded0d264 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" | |
) | |
func main() { | |
for i := 0; i <= 45; i++ { | |
fmt.Printf("%3d %10d\n", i, memFib(i)) | |
} | |
} | |
var cache = []int{1, 1} | |
func memFib(n int) int { | |
if n >= len(cache) { | |
cache = append(cache, memFib(n-2)+memFib(n-1)) | |
} | |
return cache[n] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - https://play.golang.org/p/pu-wBDLF0_
Output: