- or download this
sub fib (UInt $n) {
(state @seen = 0,1,1)[$n] //= fib($n-1) + fib($n-2);
}
- or download this
sub fib (UInt $n, UInt $curr = 0, UInt $next = 1) {
return $curr unless $n;
return fib($n-1, $next, $curr + $next);
}
- or download this
multi fib (0) { 0 }
multi fib (1) { 1 }
multi fib (UInt $n) { fib($n-1) + fib($n-2); }