Memoized fibbonacci
Signed-off-by: Xe Iaso <me@christine.website>
This commit is contained in:
parent
0de3e407e2
commit
1d9958f2e5
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue