aboutsummaryrefslogtreecommitdiff
path: root/challenge-017/jo-37/perl/ch-1.pl
blob: 18f08fbacd0ee4dd4f06a5c508e74813f1208465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}