Help for this page

Select Code to Download


  1. or download this
    sub fib (UInt $n) {
        (state @seen = 0,1,1)[$n] //= fib($n-1) + fib($n-2);
    }
    
  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);
    }
    
  3. or download this
    multi fib (0)       { 0 }
    multi fib (1)       { 1 }
    multi fib (UInt $n) { fib($n-1) + fib($n-2); }