aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Hamlin <1197072+myrrhlin@users.noreply.github.com>2019-07-16 22:36:20 -0400
committerMichael Hamlin <1197072+myrrhlin@users.noreply.github.com>2019-07-18 10:27:11 -0400
commit03ac841beabdc070b314c7b0e6765cd82bfae8c2 (patch)
treedaa0128e1003e352ef465a284b972ff3baa18180
parentd8c7f4a22934aef0f6359788cebacada3afe70ea (diff)
downloadperlweeklychallenge-club-03ac841beabdc070b314c7b0e6765cd82bfae8c2.tar.gz
perlweeklychallenge-club-03ac841beabdc070b314c7b0e6765cd82bfae8c2.tar.bz2
perlweeklychallenge-club-03ac841beabdc070b314c7b0e6765cd82bfae8c2.zip
simple approach to ackermann
l---------challenge-017/michael-hamlin/perl5/ch-1.pl1
-rw-r--r--challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl35
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-017/michael-hamlin/perl5/ch-1.pl b/challenge-017/michael-hamlin/perl5/ch-1.pl
new file mode 120000
index 0000000000..157cbc0453
--- /dev/null
+++ b/challenge-017/michael-hamlin/perl5/ch-1.pl
@@ -0,0 +1 @@
+t1-ackermann-simple.pl \ No newline at end of file
diff --git a/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl b/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl
new file mode 100644
index 0000000000..02e03a644f
--- /dev/null
+++ b/challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl
@@ -0,0 +1,35 @@
+#! /usr/bin/env perl
+#
+
+use 5.22.0;
+use warnings;
+use feature 'signatures';
+no warnings 'experimental::signatures';
+use integer;
+
+# really need the big integers when $m >= 4
+# use bigint lib => 'GMP';
+
+no warnings 'recursion';
+
+my $count = 0;
+
+sub _ack( $m, $n ) {
+ $count++;
+ if ($m > 0) {
+ return _ack( $m - 1, 1 ) unless $n;
+ return _ack( $m - 1, _ack( $m, $n - 1));
+ } else {
+ return $n + 1;
+ }
+}
+sub ack ( $m, $n ) {
+ die "the function is not defined for negative parameters" if $m < 0 || $n < 0;
+ return _ack($m, $n);
+}
+
+die "must give two nonnegative integers as input" unless @ARGV > 1;
+
+$" = ', ';
+say "A( @ARGV ) = ", ack(@ARGV), " ...after $count calls.";
+