Ackermann's Function Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For Ackermann's Function
ackermann.awka
# $Id: ackermann.gawk,v 1.6 2001/05/20 06:12:59 doug Exp $
# http://www.bagley.org/~doug/shootout/

function ack(m, n) {
    if (m == 0) return( n + 1 );
    if (n == 0) return( ack(m - 1, 1) );
    return( ack(m - 1, ack(m, (n - 1))) );
}

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];
    printf("Ack(3,%d): %d\n", n, ack(3, n));
    exit;
}
ackermann.bcc
/* -*- mode: c -*-
 * $Id: ackermann.gcc,v 1.5 2001/05/04 01:21:38 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int 
Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}


ackermann.bigforth
\ $Id: ackermann.bigforth,v 1.2 2001/06/25 00:34:05 doug Exp $
\ ackermann's function
\ http://www.bagley.org/~doug/shootout/
\ from James Hague

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

: ack  
   over 0=
   if nip 1+
   else
       dup 0=
       if drop 1- 1 recurse
       else
           1- over swap recurse  swap 1- swap recurse
       then
   then ;

\ run ack(3, NUM) and print result from stack
." Ack: " 3 NUM ack 1 u.r cr

bye
ackermann.cim
% $Id: ackermann.cim,v 1.2 2002/05/16 15:30:00 dada Exp $
external class UNIX;
begin

    integer procedure Ack(m, n); integer m, n;
    begin
        if m = 0 then Ack := n + 1        
        else if n = 0 then Ack := Ack(m-1, 1)
        else Ack := Ack(m-1, Ack(m, n-1));
    end;

    integer i;
    text t;
    t :- Blanks(80);
    t := Arg(1);
    comment OutInt(Argc, 4);
    comment OutText(Arg(1));    
    i := t.getInt;
    if i < 1 then i := 1;
    OutText("Ack(3,");
    OutInt(i, 0);
    OutText("): ");
    OutInt(Ack(3, i), 0);
    OutImage;
end
ackermann.csharp
// $Id: ackermann.csharp,v 1.0 2002/02/14 10:58:00 dada Exp $
// http://dada.perl.it/shootout/

using System;

class App {

    public static int Ack(int M, int N) {
        if (M == 0) return( N + 1 );
        if (N == 0) return( Ack(M - 1, 1) );
        return( Ack(M - 1, Ack(M, (N - 1))) );
    }
    
    public static int Main(String[] args) {
        int n;
        n = System.Convert.ToInt32(args[0]);
        if(n < 1) n = 1;
    
        Console.WriteLine("Ack(3," + n.ToString() + "): " + System.Convert.ToString(Ack(3, n)) + "\n");
        return(0);
    }
}
ackermann.cygperl
#!/usr/local/bin/perl
# $Id: ackermann.perl,v 1.9 2001/05/04 01:21:38 doug Exp $
# http://www.bagley.org/~doug/shootout/
use strict;
use integer;

# It's prettier but slower to do this
#sub Ack {
#    my($M, $N) = @_;
#    return( $N + 1 )         if ($M == 0);
#    return( Ack($M - 1, 1) ) if ($N == 0);
#    Ack($M - 1, Ack($M, $N - 1));
#}

# in our quest for speed, we must get ugly:
# it helps reduce stack frame size a little bit
# from Leif Stensson
sub Ack {
    return $_[0] ? ($_[1] ? Ack($_[0]-1, Ack($_[0], $_[1]-1))
            : Ack($_[0]-1, 1))
    : $_[1]+1;
}

my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);
my $ack = Ack(3, $NUM);
print "Ack(3,$NUM): $ack\n";
ackermann.delphi
program Ackermann;



function Ack(M, N: cardinal): cardinal;
begin
  if M = 0 then Ack := N+1
  else if N = 0 then Ack := Ack(M-1, 1)
  else Ack := Ack(M-1, Ack(M, N-1))
End;

var code: integer; NUM, a : cardinal;

begin
  NUM := 1;
  if ParamCount=1 then
    val(ParamStr(1),NUM,Code);
  a := Ack(3, NUM);
  WriteLn('Ack(3,',NUM,'): ',a);
end.
ackermann.elastic
// $Id: ackermann.elastic,v 1.0 2002/04/30 10:49:00 dada Exp $
package ackermann;

import basic;
import sys;
import array;

private function Ack( M, N )
{
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}


private n = 1;
if(array.length(sys.args) > 0) {
    n = basic.int(sys.args[0]);
} else {
    n = 1;
}
basic.print("Ack(3,", n, "): ", Ack(3, n));


ackermann.erlang
%%% -*- mode: erlang -*-
%%% $Id: ackermann.erlang,v 1.6 2000/10/07 08:41:43 doug Exp $
%%% http://www.bagley.org/~doug/shootout/

-module(ackermann).
-export([main/1]).

main() -> main(['1']).
main([Arg]) ->
    Num = list_to_integer(atom_to_list(Arg)),
    io:fwrite("Ack(3,~w): ~w\n", [Num, ack(3, Num)]),
    halt(0).

ack(0, N) -> N+1;
ack(M, 0) -> ack(M-1, 1);
ack(M, N) -> ack(M-1, ack(M, N-1)).
ackermann.fpascal
program ackermann;
uses SysUtils;

function Ack(M, N : integer) : integer;
begin    
    if M = 0 then Ack := N+1
    else if N = 0 then Ack := Ack(M-1, 1)
    else Ack := Ack(M-1, Ack(M, N-1))
End;

var NUM, a : integer;

begin
    if ParamCount = 0 then
        NUM := 1
    else
        NUM := StrToInt(ParamStr(1));
        
    if NUM < 1 then NUM := 1;
    a := Ack(3, NUM);
    WriteLn( 'Ack(3,' + IntToStr(NUM) + '): ' + IntToStr(a) );
end.
ackermann.gawk
# $Id: ackermann.gawk,v 1.6 2001/05/20 06:12:59 doug Exp $
# http://www.bagley.org/~doug/shootout/

function ack(m, n) {
    if (m == 0) return( n + 1 );
    if (n == 0) return( ack(m - 1, 1) );
    return( ack(m - 1, ack(m, (n - 1))) );
}

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];
    printf("Ack(3,%d): %d\n", n, ack(3, n));
    exit;
}
ackermann.gcc
/* -*- mode: c -*-
 * $Id: ackermann.gcc,v 1.5 2001/05/04 01:21:38 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int 
Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}


ackermann.gforth
\ $Id: ackermann.gforth,v 1.2 2001/05/25 16:43:25 doug Exp $
\ ackermann's function
\ http://www.bagley.org/~doug/shootout/

\ read NUM from last command line argument
0. argc @ 1- arg >number 2drop drop constant NUM

: ack  recursive
    dup 0=
    if
    drop 1+
    else
    swap dup 0=
    if
        drop 1- 1 swap ack
    else
        1- over 1- rot rot swap ack swap ack
    then
    then ;
\ END ACK

\ run ack(3, NUM) and print result from stack
." Ack: " NUM 3 ack 1 u.r cr

bye \ th-th-that's all folks!
ackermann.ghc
-- $Id: ackermann.ghc,v 1.5 2001/06/16 01:33:06 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- shortened by Bryn Keller

import System(getArgs)

main = do
       ~[number] <- getArgs
       let n = read number in
       putStrLn ("Ack(3," ++ (show n) ++ "): " ++ (show (ack 3 n)))

ack :: Int -> Int -> Int
ack 0 n = n+1
ack m 0 = ack (m-1) 1
ack m n = ack (m-1) (ack m (n-1));
ackermann.gnat
-- $Id: ackermann.gnat,v 1.0 2003/06/11 12:01:00 dada Exp $
-- http://dada.perl.it/shootout/
-- Ada 95 code by C.C.

with Ada.Command_Line, Text_IO, Ada.Strings.Fixed, Interfaces;

procedure Ackermann is
   function L_Trim (Source : String; Side : Ada.Strings.Trim_End :=
               Ada.Strings.Left) return String renames Ada.Strings.Fixed.Trim;

   subtype Nat is Interfaces.Integer_32 range 0 .. Interfaces.Integer_32'Last;
   subtype Pos is Nat range 1 .. Nat'Last;         --  Largest is 2_147_483_647
   use type Nat;

   function Ack (M, N : Nat) return Pos is
      pragma Suppress (Overflow_Check);
      pragma Suppress (Range_Check);
   begin
      if M = 0 then
         return N + 1;
      elsif N = 0 then
         return Ack (M - 1, 1);
      else
         return Ack (M - 1, Ack (M, N - 1));
      end if;
   end Ack;

   N        : Pos := 1;
begin
   if Ada.Command_Line.Argument_Count > 0 then
      N := Pos'Value (Ada.Command_Line.Argument (1));
   end if;
   Text_IO.Put_Line ("Ack(3," & L_Trim (Pos'Image (N)) & "):" &
            Pos'Image (Ack (3, N)));
end Ackermann;

ackermann.guile
#!/usr/local/bin/guile \
-e main -s
!#
;;; $Id: ackermann.guile,v 1.9 2001/06/29 23:12:36 doug Exp $
;;; http://www.bagley.org/~doug/shootout/

(debug-set! stack 0)

(define (main args)
  (let ((n (or (and (= (length args) 2) (string->;number (cadr args))) 1)))
    (print-list "Ack(3," n "): " (ack 3 n))))

(define (ack m n)
  (cond ((zero? m) (+ n 1))
    ((zero? n) (ack (- m 1) 1))
    (else      (ack (- m 1) (ack m (- n 1))))))

(define (print-list . items) (for-each display items) (newline))
ackermann.ici
// $Id: ackermann.ici,v 1.0 2003/01/03 11:12:00 dada Exp $
// http://dada.perl.it/shootout
//
// contributed by Tim Long

static
Ack(M, N)
{
    return M ? (Ack(M - 1, N ? Ack(M, N - 1) : 1)) : N + 1;
}

n := argv[1] ? int(argv[1]) : 1;
printf("Ack(3,%d): %d\n", n, Ack(3, n));
ackermann.icon
# -*- mode: icon -*-
# $Id: ackermann.icon,v 1.2 2000/12/17 23:34:17 doug Exp $
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
    num := argv[1];
    write("Ack(3,", num, "): ", ack(3,num));
end

procedure ack(m,n)
    if (m == 0) then {
    return(n + 1)
    }
    if (n == 0) then {
    return(ack(m-1, 1))
    }
    return(ack(m-1, ack(m, n-1)))
end
ackermann.java
// $Id: ackermann.java,v 1.4 2000/10/07 08:41:43 doug Exp $
// http://www.bagley.org/~doug/shootout/

public class ackermann {

    public static void main(String args[]) {
    int NUM = Integer.parseInt(args[0]);
    System.out.print("Ack(3," + NUM + "): " + Ack(3, NUM) + "\n");
    }

    public static int Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
    }

}

ackermann.jscript
function Ack(M, N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

var n;

ARGS = WScript.Arguments;

if(ARGS.length > 0) {
  n = parseInt(ARGS.Item(0), "10");
  if(n < 1) n = 1;
} else {   
  n = 1;
}

WScript.Echo("Ack(3,"+ n+ "):", Ack(3, n));

ackermann.lcc
/* -*- mode: c -*-
 * $Id: ackermann.gcc,v 1.5 2001/05/04 01:21:38 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int 
Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}


ackermann.lua
-- $Id: ackermann.lua,v 1.5 2000/12/09 20:07:43 doug Exp $
-- http://www.bagley.org/~doug/shootout/

function Ack(M, N)
    if (M == 0) then
    return( N + 1 )
    end
    if (N == 0) then
    return( Ack(M - 1, 1) )
    end
    return( Ack(M - 1, Ack(M, (N - 1))) )
end

NUM = tonumber((arg and arg[1])) or 1
write("Ack(3,", NUM ,"): ", Ack(3,NUM), "\n")

ackermann.lua5
-- $Id: ackermann.lua,v 1.5 2000/12/09 20:07:43 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- contributed by Roberto Ierusalimschy

function Ack(M, N)
  if M == 0 then
    return N + 1
  elseif N == 0 then
    return Ack(M - 1, 1)
  else
    return Ack(M - 1, Ack(M, (N - 1)))
  end
end

NUM = tonumber((arg and arg[1])) or 1
io.write("Ack(3,", NUM ,"): ", Ack(3,NUM), "\n")

ackermann.mawk
# $Id: ackermann.mawk,v 1.2 2001/05/20 06:12:59 doug Exp $
# http://www.bagley.org/~doug/shootout/

function ack(m, n) {
    if (m == 0) return( n + 1 );
    if (n == 0) return( ack(m - 1, 1) );
    return( ack(m - 1, ack(m, (n - 1))) );
}

BEGIN {
    n = (ARGV[1] < 1) ? 1 : ARGV[1];
    printf("Ack(3,%d): %d\n", n, ack(3, n));
    exit;
}
ackermann.mercury
%% $Id: ackermann.mercury,v 1.3 2001/05/13 01:22:35 doug Exp $
%% http://www.bagley.org/~doug/shootout/

:- module mytest.

:- interface.

:- import_module io.

:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

:- implementation.

:- func ack(int, int) = int.
:- mode ack(in, in) = out is det.

:- import_module string, list, int.

:- pred mytest(int, io__state, io__state).
:- mode mytest(in, di, uo) is det.

ack(M, N) = R :- 
    ( if M = 0 then
    R = N + 1
      else if N = 0 then
    R = ack(M - 1, 1)
      else
    R = ack(M - 1, ack(M, N - 1))
    ).

mytest(Num) -->
    io__write_string("Ack(3,"),
    io__write_int(Num),
    io__write_string("): "),
    io__write_int(ack(3,Num)),
    io__write_string("\n").

main -->
    io__command_line_arguments(Args),
    ( if { Args = [] } then
         mytest(1)
      else if { Args = [Arg|_] } then
         ( if { string__to_int(Arg, N), N > 0 } then
             mytest(N)
       else
             mytest(1)
         )
    ).

ackermann.mingw32
/* -*- mode: c -*-
 * $Id: ackermann.gcc,v 1.5 2001/05/04 01:21:38 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int 
Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}


ackermann.modula2
(* The Great Win32 Language Shootout http://dada.perl.it/shootout/

   contributed by Isaac Gouy (Modula2 novice)

   To build: xc =m ackermann
   To run:     ackermann 8
*)

MODULE Ackermann;

(* Prefer unqualified procedure import *)
FROM LanguageShootout IMPORT N;

FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;


PROCEDURE Ack (m, n: CARDINAL) : CARDINAL;
BEGIN
   IF m=0 THEN RETURN n+1; END;
   IF n=0 THEN RETURN Ack(m-1, 1); END;
   RETURN Ack(m-1, Ack(m, n-1));
END Ack;


VAR n: CARDINAL;

BEGIN
  n := N();

  WriteString("Ack(3," ); WriteCard(n,1); WriteString("): ");
  WriteCard( Ack(3,n), 1); WriteLn;
END Ackermann.
ackermann.modula3

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

PROCEDURE Ack(M, N: INTEGER): INTEGER =
BEGIN
    IF M = 0 THEN 
        RETURN N+1;
    ELSE
        IF N = 0 THEN
            RETURN Ack(M-1, 1);
        ELSE
            RETURN Ack(M-1, Ack(M, N-1));
        END;
    END;
END Ack;

VAR N: INTEGER;
BEGIN
    IF Params.Count > 0 THEN
        N := Scan.Int(Params.Get(1));
    ELSE
        N := 1;
    END;
    Wr.PutText (Stdio.stdout, "Ack(3," & Fmt.Int(N) & "): " & Fmt.Int(Ack(3, N)));
    Wr.Close (Stdio.stdout);
END Main.
ackermann.nice
/* The Great Win32 Language Shootout http://dada.perl.it/shootout/ 
   contributed by Isaac Gouy (Nice novice)

To compile:	
   nicec --sourcepath=.. -d=. -a ackermann.jar ackermann

To run:
   java -jar ackermann.jar 8
*/


void main(String[] args){

   // NOTE: the type of n will be 
   //       inferred by the compiler

   let n = toSingleInt(args);   
   println("Ack(3," + n + "): " + ack(3,n)); 
}


int ack(int m, int n){
   if (m == 0) return n + 1;
   if (n == 0) return ack(m-1, 1);
   return ack(m-1, ack(m, n-1)); 
}


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }
ackermann.ocaml
(*
 * $Id: ackermann.ocaml,v 1.9 2001/01/08 02:56:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * with help from Markus Mottl
 *)


let rec ack m n =
  if m = 0 then n + 1
  else if n = 0 then ack (m - 1) 1
  else ack (m - 1) (ack m (n - 1))

let _ =
  let arg =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  Printf.printf "Ack(3,%d): %d\n" arg (ack 3 arg)

ackermann.ocamlb
(*
 * $Id: ackermann.ocaml,v 1.9 2001/01/08 02:56:48 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * with help from Markus Mottl
 *)


let rec ack m n =
  if m = 0 then n + 1
  else if n = 0 then ack (m - 1) 1
  else ack (m - 1) (ack m (n - 1))

let _ =
  let arg =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  Printf.printf "Ack(3,%d): %d\n" arg (ack 3 arg)

ackermann.oz
%%% $Id: ackermann.oz,v 1.0 2002/03/11 14:41:00 dada Exp $
%%% http://dada.perl.it/shootout/
functor
import System Application
define
fun {Ack M N}
    if M==0 then N + 1
    elseif N == 0 then {Ack M-1 1}
    else {Ack M-1 {Ack M N-1}}
    end
end
in 
    local A in
        [A] = {Application.getArgs plain}
        {System.printInfo "Ack(3,"}
        {System.printInfo A}
        {System.printInfo "): "}
        {System.printInfo {Ack 3 {String.toInt A}}}
    end
    {Application.exit 0}
end
ackermann.parrot

ACKERMANN:
    set I1, 3
    set I2, P0[1]
    bsr ACK
    print "Ack("
    print I1
    print ","
    print I2
    print "): "
    print I0
    print "\n"
    end

ACK:
    save I1
    save I2
    unless I1, ACK_M0
    unless I2, ACK_N0

    dec I2
    bsr ACK
    set I2, I0
    dec I1
    bsr ACK
    branch ACK_END

ACK_M0:
    set I0, I2
    inc I0
    branch ACK_END

ACK_N0:
    dec I1
    set I2, 1
    bsr ACK

ACK_END:    
    restore I2
    restore I1            
    ret

ackermann.perl
#!/usr/local/bin/perl
# $Id: ackermann.perl,v 1.9 2001/05/04 01:21:38 doug Exp $
# http://www.bagley.org/~doug/shootout/
use strict;
use integer;

# It's prettier but slower to do this
#sub Ack {
#    my($M, $N) = @_;
#    return( $N + 1 )         if ($M == 0);
#    return( Ack($M - 1, 1) ) if ($N == 0);
#    Ack($M - 1, Ack($M, $N - 1));
#}

# in our quest for speed, we must get ugly:
# it helps reduce stack frame size a little bit
# from Leif Stensson
sub Ack {
    return $_[0] ? ($_[1] ? Ack($_[0]-1, Ack($_[0], $_[1]-1))
            : Ack($_[0]-1, 1))
    : $_[1]+1;
}

my $NUM = $ARGV[0];
$NUM = 1 if ($NUM < 1);
my $ack = Ack(3, $NUM);
print "Ack(3,$NUM): $ack\n";
ackermann.php
<?php
/*
 $Id: ackermann.php,v 1.1 2001/04/16 18:45:12 doug Exp $
 http://www.bagley.org/~doug/shootout/
 from Alexander Klimov
*/
function Ack($m, $n){
  if($m == 0) return $n+1;
  if($n == 0) return Ack($m-1, 1);
  return Ack($m - 1, Ack($m, ($n - 1)));
}
$n = ($argc == 2) ? $argv[1] : 1;
$r = Ack(3,$n);
print "Ack(3,$n): $r\n";
?>
ackermann.pike
#!/usr/local/bin/pike// -*- mode: pike -*-
// $Id: ackermann.pike,v 1.8 2001/07/08 19:59:15 doug Exp $
// http://www.bagley.org/~doug/shootout/

int Ack(int M, int N) {
  if (M == 0) return( N + 1 );
  if (N == 0) return( Ack(M - 1, 1) );
  return( Ack(M - 1, Ack(M, (N - 1))) );
}

int main(int argc, array(string) argv) {
  int N = (int)argv[-1] || 1;
  write("Ack(3,%d): %d\n", N, Ack(3, N));
}
ackermann.pliant
module "/pliant/language/context.pli"

function ack m n -> r
  arg Int m n r
  if m=0
    return (n + 1)
  eif n=0
    return (ack m-1 1)
  else
    return (ack m-1 (ack m n-1))

gvar Str s_n := cast ((pliant_script_args translate Address 1) map CStr) Str
if (s_n parse (gvar Int n))
  console "Ack(3," n "): " (ack 3 n) eol
else
  console "usage: ack.pli <number>" eol
ackermann.poplisp
;;; -*- mode: lisp -*-
;;; $Id: ackermann.poplisp,v 1.0 2002/05/03 12:03:00 dada Exp $

(defun fast-ack (m n)
  (declare (fixnum n m) (optimize (speed 3) (debug 0) (safety 0)))
  (the fixnum
    (cond
     ((zerop m) (the fixnum (1+ n)))
     ((zerop n) (the fixnum (fast-ack (1- m) 1)))
     (t (the fixnum (fast-ack (1- m) (the fixnum (fast-ack m (1- n)))))))))

;(defun ack (m n)
;  (cond
;   ((zerop m) (1+ n))
;   ((zerop n) (ack (1- m) 1))
;   (t (ack (1- m) (ack m (1- n))))))

(let ((n (parse-integer (or (car pop11::poparglist) "1"))))   
    (format *standard-output* "Ack(3,~A): ~A~%" n (fast-ack 3 n))     
)
ackermann.python
#!/usr/local/bin/python
# $Id: ackermann.python,v 1.7 2001/03/16 15:57:34 doug Exp $
# http://www.bagley.org/~doug/shootout/
# from Brad Knotwell

import sys

def Ack(M, N):
    if (not M):
        return( N + 1 )
    if (not N):
        return( Ack(M-1, 1) )
    return( Ack(M-1, Ack(M, N-1)) )

def main():
    NUM = int(sys.argv[1])
    sys.setrecursionlimit(3000)
    print "Ack(3,%d): %d" % (NUM, Ack(3, NUM))

main()

ackermann.rebol
REBOL [
    Title:   "Ackermann"
    Author:  "Aldo Calpini"
    Date:    03-Jul-2001
    File:    %ackermann.r
]

Ack: func [M N] [
    return either M > 0  
        [ either N > 0 
            [ Ack M - 1 Ack M N - 1 ]
            [ Ack M - 1 1 ]
        ]
        [ N + 1 ]
]

NUM: to-integer to-string first system/script/args
NUM: either NUM < 1 [ 1 ] [ NUM ]
R: Ack 3 NUM
print rejoin [ "Ack(3," NUM "): " R ]
ackermann.rexx

parse arg n
If n < 1 Then Do
    n = 1
End

R = Ack(3,N)

say "Ack(3,"n"): "R
exit

Ack:
    PROCEDURE
    PARSE ARG M,N
    IF M<1 THEN RETURN N+1
    IF N<1 THEN RETURN Ack(M-1, 1)
    R1 = Ack(M, N-1)
    RETURN Ack(M-1, R1)    
    
ackermann.ruby
#!/usr/local/bin/ruby
# -*- mode: ruby -*-
# $Id: ackermann.ruby,v 1.6 2000/11/27 03:39:25 doug Exp $
# http://www.bagley.org/~doug/shootout/

def ack(m, n)
    if m == 0 then
    n + 1
    elsif n == 0 then
    ack(m - 1, 1)
    else
    ack(m - 1, ack(m, n - 1))
    end
end

NUM = Integer(ARGV.shift || 1)
print "Ack(3,", NUM, "): ", ack(3, NUM), "\n"

ackermann.se
-- -*- mode: eiffel -*-
-- $Id: ackermann.se,v 1.1 2000/10/12 07:11:42 doug Exp $
-- http://www.bagley.org/~doug/shootout/

class ACKERMANN

creation make

feature

   make is

      local
     num: INTEGER;
      do
     if argument_count = 1 then
        num := argument(1).to_integer
     else
        num := 1
     end
         std_output.put_string("Ack(3,")
         std_output.put_integer(num)
         std_output.put_string("): ")
         std_output.put_integer(ack(3, num))
         std_output.put_new_line
      end;

feature {NONE}

   ack(m, n:INTEGER):INTEGER is
      do
     if m = 0 then
        Result := n + 1
     elseif n = 0 then
        Result := ack(m - 1, 1)
     else
        Result := ack(m - 1, ack(m, (n - 1)))
     end
      end;
end
ackermann.slang
% $Id: ackermann.slang,v 1.0 2002/11/29 10:39:00 dada Exp $
% http://dada.perl.it/shootout/
%
% contributed by John E. Davis

define ack ();
define ack (M, N)
{
   !if (M)
     return( N + 1 );
   !if (N)
     return ( ack(M-1, 1) );
   return( ack(M-1, ack(M, N-1)) );
}


define main()
{
   variable NUM = integer (__argv[1]);
   vmessage ("Ack(3,%d): %d", (NUM, ack(3, NUM)));
}

main()
ackermann.smlnj
(* -*- mode: sml -*-
 * $Id: ackermann.smlnj,v 1.5 2001/07/09 00:25:27 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 *)

structure Test : sig
    val main : (string * string list) -> OS.Process.status
end = struct

fun ack(0,n) = n+1
  | ack(m,0) = ack(m-1,1)
  | ack(m,n) = ack(m-1,ack(m,(n-1)));

fun atoi s = case Int.fromString s of SOME num => num | NONE => 0;
fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t );

fun main(name, args) = 
  let
    val arg = hd(args @ ["1"]);
    val num = atoi arg;
    val ack = ack(3, num);
    val result = Int.toString ack;
  in
      printl ["Ack(3,", arg, "): ", result];
      OS.Process.success
  end
end

val _ = SMLofNJ.exportFn("ackermann", Test.main);
ackermann.tcl
#!/usr/local/bin/tclsh
# $Id: ackermann.tcl,v 1.3 2000/12/30 18:58:30 doug Exp $
# http://www.bagley.org/~doug/shootout/

set NUM [lindex $argv 0]
if {$NUM < 1} {
    set NUM 1
}

proc ack {m n} {
    if {$m == 0} {
    return [expr {$n + 1}]
    } elseif {$n == 0} {
    return [ack [expr {$m - 1}] 1]
    } else {
    return [ack [expr {$m - 1}] [ack $m [expr {$n - 1}]]]
    }
}

set ack [ack 3 $NUM]
puts "Ack(3,$NUM): $ack"
ackermann.vbscript
Function Ack(M, N)
    If M = 0 Then
        Ack = N + 1
    Else
        If N = 0 Then
            Ack = Ack(M-1, 1)
        Else
            Ack = Ack(M-1, Ack(M, N-1))
        End If
    End If
End Function

NUM = WScript.Arguments(0)
If NUM < 1 Then NUM = 1
nack = Ack(3, NUM)
WScript.Echo "Ack(3," & NUM & "): " & nack
ackermann.vc
/* -*- mode: c -*-
 * $Id: ackermann.gcc,v 1.5 2001/05/04 01:21:38 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 */

#include <stdio.h>
#include <stdlib.h>

int 
Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int
main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    printf("Ack(3,%d): %d\n", n, Ack(3, n));
    return(0);
}


ackermann.vc++
// -*- mode: c++ -*-
// $Id: ackermann.g++,v 1.3 2001/06/20 03:20:02 doug Exp $
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>

using namespace std;

int Ack(int M, int N) {
    if (M == 0) return( N + 1 );
    if (N == 0) return( Ack(M - 1, 1) );
    return( Ack(M - 1, Ack(M, (N - 1))) );
}

int main(int argc, char *argv[]) {
    int n = ((argc == 2) ? atoi(argv[1]) : 1);

    cout << "Ack(3," << n << "): " << Ack(3, n) << endl;
    return(0);
}
ackermann.vpascal
program ackermann;
uses SysUtils;

function Ack(M, N : integer) : integer;
begin    
    if M = 0 then Ack := N+1
    else if N = 0 then Ack := Ack(M-1, 1)
    else Ack := Ack(M-1, Ack(M, N-1))
End;

var NUM, a : integer;

begin
    if ParamCount = 0 then
        NUM := 1
    else
        NUM := StrToInt(ParamStr(1));
        
    if NUM < 1 then NUM := 1;
    a := Ack(3, NUM);
    WriteLn( 'Ack(3,' + IntToStr(NUM) + '): ' + IntToStr(a) );
end.