Memoized fibbonacci

Signed-off-by: Xe Iaso <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-04-24 23:50:35 +00:00
parent 0de3e407e2
commit 1d9958f2e5
1 changed files with 18 additions and 0 deletions

View File

@ -15,6 +15,24 @@ func TestRecurFib(t *testing.T) {
t.Log(Fib(40))
}
func TestMemoizedFib(t *testing.T) {
mem := map[int]int{
0: 0,
1: 1,
}
var fib func(int) int
fib = func(n int) int {
if result, ok := mem[n]; ok {
return result
}
result := fib(n-1) + fib(n-2)
mem[n] = result
return result
}
}
func TestThunkFib(t *testing.T) {
cache := make([]*Thunk[int], 41)