diff options
author | Guillermo Ramos | 2019-07-15 20:03:19 +0200 |
---|---|---|
committer | Guillermo Ramos | 2019-07-15 20:03:19 +0200 |
commit | 15c4c7c55c7cf541c6fc0142ac216a94b3b7bcb3 (patch) | |
tree | 5ba13f5d799f6048335a0ec593bfce080aa469d9 /017/ch1.pl | |
parent | 123b23627bddfe9a87a9d19a96e6b4ccb9c9062d (diff) | |
download | perlweekly-15c4c7c55c7cf541c6fc0142ac216a94b3b7bcb3.tar.gz |
[017#1]
Diffstat (limited to '017/ch1.pl')
-rwxr-xr-x | 017/ch1.pl | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/017/ch1.pl b/017/ch1.pl new file mode 100755 index 0000000..6a79051 --- /dev/null +++ b/017/ch1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +# +# Create a script to demonstrate Ackermann function. The Ackermann function is +# defined as below, m and n are positive number: +# +# A(m, n) = n + 1 if m = 0 +# A(m, n) = A(m - 1, 1) if m > 0 and n = 0 +# A(m, n) = A(m - 1, A(m, n - 1)) if m > 0 and n > 0 +# +# Example expansions as shown in wiki page. +# +# A(1, 2) = A(0, A(1, 1)) +# = A(0, A(0, A(1, 0))) +# = A(0, A(0, A(0, 1))) +# = A(0, A(0, 2)) +# = A(0, 3) +# = 4 +# +# (https://en.wikipedia.org/wiki/Ackermann_function). +################################################################################ + +use strict; +use warnings; + +use Memoize; + +memoize "ack"; + +sub ack { + my ($m, $n) = @_; + return $n+1 if $m == 0; + return ack($m-1, 1) if $n == 0; + return ack($m-1, ack($m, $n-1)); +} + +@ARGV == 2 || die "Usage: $0 <m> <n>\n"; +print ack(@ARGV), "\n"; |