in reply to Re: memory leak
in thread memory leak

there are many forms for Ackermanns function in this case I have used the following function A(1,j) = 2^j for j>=1, A(i,1) = A(i-1,2) for i>=2, A(i,j) = A(i-1,A(i,j-1)) for i,j>=2

Replies are listed 'Best First'.
Re^3: memory leak
by Laurent_R (Canon) on Jul 28, 2013 at 15:54 UTC

    OK, I have tried reimplementing your Ackermann function, still with a limit at 40 iterations to see where we get:

    use strict; use warnings; use Math::BigInt; no warnings 'recursion'; my $iter = 0; my ($c, $d) = @ARGV; print "ackermann ( $c, $d) = ", acker($c, $d), "\n"; sub acker { my ($i, $j) = @_; $iter++; print "Iteration $iter: i = $i, j = $j \n"; return if $iter >= 40; return 2**$j if $i <= 1; return acker( $i-1, 2) if $j <= 1; return acker ($i - 1, acker ($i, $j - 1)); }

    It works for 2 and 3:

    $ perl pseudo_acker2.pl 2 3 Iteration 1: i = 2, j = 3 Iteration 2: i = 2, j = 2 Iteration 3: i = 2, j = 1 Iteration 4: i = 1, j = 2 Iteration 5: i = 1, j = 4 Iteration 6: i = 1, j = 16 ackermann ( 2, 3) = 65536

    But it ceases to work for 2 and 4:

    $ perl pseudo_acker2.pl 2 4 Iteration 1: i = 2, j = 4 Iteration 2: i = 2, j = 3 Iteration 3: i = 2, j = 2 Iteration 4: i = 2, j = 1 Iteration 5: i = 1, j = 2 Iteration 6: i = 1, j = 4 Iteration 7: i = 1, j = 16 Iteration 8: i = 1, j = 65536 ackermann ( 2, 4) = inf

    In short, your Ackermann function seems to be broken or, rather, to grow so fast as to become very quickly unmanageable.

    You could also try to use one of the big integer modules, but even then your Ackerman function is more or less unusable. Using the bigint module on values 2 and 4, you get a number with almost 20,000 digits:

    $ perl pseudo_acker2.pl 2 4 Iteration 1: i = 2, j = 4 Iteration 2: i = 2, j = 3 Iteration 3: i = 2, j = 2 Iteration 4: i = 2, j = 1 Iteration 5: i = 1, j = 2 Iteration 6: i = 1, j = 4 Iteration 7: i = 1, j = 16 Iteration 8: i = 1, j = 65536 ackermann ( 2, 4) = 20035299304068464649790723515602557504478254755697 +5141926501697371089405955631145308950613088093334810103 8234342907263181822949382118812668869506364761547029165041871916351587 +96634721944293092798208430910485599 0570159318959639524863372367203002916969592156108764948889254090805911 +45703767520850020667156370236612635 9747144807111774815880914135742720967190151836282560618091458852699826 +14142503012339110827360384376787644 9043205960379124490905707560314035076162562476031863793126484703743782 +95497561377098160461441330869211810