From 03ac841beabdc070b314c7b0e6765cd82bfae8c2 Mon Sep 17 00:00:00 2001 From: Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> Date: Tue, 16 Jul 2019 22:36:20 -0400 Subject: simple approach to ackermann --- challenge-017/michael-hamlin/perl5/ch-1.pl | 1 + .../michael-hamlin/perl5/t1-ackermann-simple.pl | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 120000 challenge-017/michael-hamlin/perl5/ch-1.pl create mode 100644 challenge-017/michael-hamlin/perl5/t1-ackermann-simple.pl 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."; + -- cgit