[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Ackerman



Do you think your computer has too much memory ? Give it a workout!


#!perl -X
use strict;
use Math::BigInt;

# $\=$/;

sub Acker
{
   my ( $m, $n ) = @_;
   if( $m==0 ){
      return $n+1;
   }
   if( $n==0 ){
      return Acker( $m-1, 0 );
   }
   return Acker( $m-1, Acker($m,$n-1) );
}

my $level = 0;
my $maxx;
my $maxy;

sub A
{
   my ( $x, $y ) = @_;
   $maxx = $x if( $maxx < $x );
   $maxy = $y if( $maxy < $y );
   #   $level ++;
   #   print ' ' x $level , "Ackerman for $x, $y\n";
   my $rez = ($x>0) ? A($x-1,($y>0)?A($x,$y-1):1) : $y+1;
   #   $level --;
   $rez;
}

sub sigint{
   my ($signal) = @_;
   die "Caught SIG$signal, $maxx,$maxy";
}

$SIG{INT} = \&sigint;
$SIG{TERM} = \&sigint;
$SIG{QUIT} = \&sigint;
$SIG{KILL} = \&sigint;

for( my $i=0; $i<=4; $i++ )
{
   for( my $j=0; $j<=7; $j++ )
   {
      $maxx = $maxy = 0;
      print "A( $i, $j ) = ",A( $i, $j ),"\n";
      print "\t\t$maxx,$maxy\n";
   }
   print '-' x 20, "\n";
}

__END__

This script computes the Ackerman function, The Most Fiendishly Recursive
Function Known To Man (tm).

Perl 5.6.0 for OS/2 ran overnight until it couldn't allocate any more
memory (used up ~200 megs).
ActiveState Perl (build 613) on NT crashed :-( GPF )-:
Linux and FreeBSD are still running as I write this (Load 1.00 1.00 1.00 :
-)

--
Csaba Ráduly, Software Engineer                     Sophos Anti-Virus
email: csaba.raduly@sophos.com                  http://www.sophos.com
US support: +1 888 SOPHOS 9               UK Support: +44 1235 559933




==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe