diff options
| author | Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> | 2019-07-18 10:21:03 -0400 |
|---|---|---|
| committer | Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> | 2019-07-18 10:27:13 -0400 |
| commit | ac7e397b968aa71ff5380565e0c973f2e9d0c5a2 (patch) | |
| tree | 7a9a5c8401d1aa9b29c75cc5bfec364d834b8ec2 /challenge-017 | |
| parent | d10c5f0195d2ab291371711784373421081f268b (diff) | |
| download | perlweeklychallenge-club-ac7e397b968aa71ff5380565e0c973f2e9d0c5a2.tar.gz perlweeklychallenge-club-ac7e397b968aa71ff5380565e0c973f2e9d0c5a2.tar.bz2 perlweeklychallenge-club-ac7e397b968aa71ff5380565e0c973f2e9d0c5a2.zip | |
add test function
Diffstat (limited to 'challenge-017')
| -rw-r--r-- | challenge-017/michael-hamlin/perl5/t1-ackermann-two.pl | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/challenge-017/michael-hamlin/perl5/t1-ackermann-two.pl b/challenge-017/michael-hamlin/perl5/t1-ackermann-two.pl index a93cb0e43b..bdb6ce5e2a 100644 --- a/challenge-017/michael-hamlin/perl5/t1-ackermann-two.pl +++ b/challenge-017/michael-hamlin/perl5/t1-ackermann-two.pl @@ -11,6 +11,10 @@ no warnings 'experimental::signatures'; use bigint lib => 'GMP'; my $trace = 0; +my %limits = ( + 5 => 0, + 4 => 2, +); # the hyper function, H_$n($a, $b), with $a = 2: sub _twohyper( $n, $b ) { @@ -47,10 +51,16 @@ sub _twohyper( $n, $b ) { } sub ack2 ( $m, $n ) { die "the function is not defined for negative parameters" if $m < 0 || $n < 0; - warn "this may run out of memory..." if $m > 4 || $m == 4 && $n >= 3; + if (defined my $nlimit = $limits{$m}) { + warn "this may run out of memory..." if $n > $nlimit; + } return _twohyper($m, $n + 3) - 3; } +unless (@ARGV) { + _test(); + exit(0); +} die "must give two nonnegative integers as input" unless @ARGV > 1; $" = ', '; @@ -60,3 +70,15 @@ my $desc = $result->length < 78 ? $result : say "A( @ARGV ) = ", $desc; +sub _test { + for my $m (0 .. 5) { + my $nlimit = $limits{$m} // 5; + for my $n (0 .. $nlimit) { + my $result = ack2($m, $n); + my $desc = $result->length < 78 ? $result : + sprintf 'int with %u digits', length($result); + say "A( $m, $n ) = ", $desc; + } + } +} + |
