Skip to content

Instantly share code, notes, and snippets.

@colomon
Created March 13, 2012 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/2026007 to your computer and use it in GitHub Desktop.
Save colomon/2026007 to your computer and use it in GitHub Desktop.
multi fib($x) {
given $x {
when * < 0 { die "non-negative arguments required" }
when %memo{$x} { %memo{$x} }
default {
my $res = fib($x-1) + fib($x-2);
%memo{$x} = $res;
}
}
}
# or better yet:
my @fib := 0, 1, *+* ... *;
sub fib($x) { @fib[$x] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment