diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-15 18:24:47 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:41 +0100 |
| commit | 27746120072266d19b4aba4970e75098b6995b7d (patch) | |
| tree | 3f1da087b1d393fbb4c9e809d006cd65c8069ec5 | |
| parent | 99b63fe9ce352e50645018e03c8d6156cd166b01 (diff) | |
| download | perlweeklychallenge-club-27746120072266d19b4aba4970e75098b6995b7d.tar.gz perlweeklychallenge-club-27746120072266d19b4aba4970e75098b6995b7d.tar.bz2 perlweeklychallenge-club-27746120072266d19b4aba4970e75098b6995b7d.zip | |
Challenge 017 task 1
| -rwxr-xr-x | challenge-017/jo-37/perl/ch-1.pl | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-017/jo-37/perl/ch-1.pl b/challenge-017/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..18f08fbacd --- /dev/null +++ b/challenge-017/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Memoize; +use experimental 'signatures'; +no warnings 'recursion'; + +memoize('ackermann'); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [M N] + +-examples + run the examples from the challenge + +-tests + run some tests + +M N + calculate ackermann(M, N) + +EOS + + +### Input and Output + +say ackermann(@ARGV); + + +### Implementation + +sub ackermann ($m, $n) { + return $n + 1 unless $m; + return ackermann($m - 1, 1) unless $n; + ackermann($m - 1, ackermann($m, $n - 1)); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is ackermann(1, 2), 4; + is ackermann(2, 3), 9; + is ackermann(3, 4), 125; + is ackermann(4, 0), 13; + is ackermann(4, 1), 65533; + is ackermann(5, 0), 65533; + + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
