Exercises
-
Write a function computing values of the Fibonacci sequence, i.e., a function
function fib(n: uint64): uint64 { ... }- if
n < 2returnn - else return
fib(n - 1) + fib(n - 2)
For testing you can
assert fib(8) == 21;. - if
-
Add memoization to your
fibfunction. For that change its signature tofunction fib(n: uint64, inout cache: map<uint64, uint64>): uint64 { ... }This can then be called like so:
global m_fib: map<uint64, uint64>; fib(64, m_fib);For testing you can
assert fib(64, m_fib) == 10610209857723;. -
Try modifying your
fibfunctions so users do not have to provide the cache themselves.