/* This is a well tested program that computes one value of 3-argument Ackermann's function. 1st and 2nd argument are the numbers on which a two-argument operation is to be carried out. 3rd argument is an index of the operation. E.g., A(k, m, 0) = k + m A(k, m, 1) = k * m A(k, m, 2) = k ^ m etc. */ public class Ackerman3arg { static int count = 0; public static void main (String [] args) { int n = Integer.parseInt(args[0]); int m = Integer.parseInt(args[1]); int k = Integer.parseInt(args[2]); long a = A(n,m, k); System.out.print("A(" + n + ", " + m + ", " + k + ") = " + a); System.out.println(" count = " + count); } public static long A(long k, long m, long n) { count++; if (n == 0) return m + k; else { if (m == 0) { if (n == 1) return 0; else return 1; } else return A(k, A(k, m-1, n), n-1); } } } /* Inductive definition of the Ackermann's function: A(k, m, 0) = m + k; A(k, 0, 1) = 0 A(k, 0, n+2) = 1 A(k, m+1, n+1) = A(k, A(k, m, n+1), n) */