aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl12
1 files changed, 6 insertions, 6 deletions
diff --git a/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl b/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl
index 02e03a644f..406fe6073e 100644
--- a/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl
+++ b/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl
@@ -15,13 +15,13 @@ no warnings 'recursion';
my $count = 0;
sub _ack( $m, $n ) {
+ state @known;
$count++;
- if ($m > 0) {
- return _ack( $m - 1, 1 ) unless $n;
- return _ack( $m - 1, _ack( $m, $n - 1));
- } else {
- return $n + 1;
- }
+ return $known[$m][$n] ||= do {
+ !$m ? $n + 1 :
+ !$n ? _ack($m - 1, 1) :
+ _ack($m - 1, _ack($m, $n - 1));
+ };
}
sub ack ( $m, $n ) {
die "the function is not defined for negative parameters" if $m < 0 || $n < 0;