/***************************************************************/
/*                                                             */
/* Program: acker.c                                            */
/*                                                             */
/* Description:                                                */
/*                                                             */
/* The funny thing with Ackermanns function is that it can     */
/* not be defined without recursion!                           */
/*                                                             */
/* Algorithm from the swedish book: "Programmeringsmetodik     */
/* med abstrakta datatyper" by Goeran Eriksson and             */
/* Jan Karlsson, Lund 1986, page 70.                           */
/*                                                             */
/* The program was updated and improved in October 2003 on     */
/* suggestion by Thomas Lingefjaerd, Goeteborg University.     */
/*                                                             */
/*                                                             */
/* Program by: Stefan Spaennare, Lund, Sweden                  */
/*                                                             */
/* E-mail:      stefan@spaennare.se                            */
/*                                                             */
/* Latest update: 2003-10-09                                   */
/*                                                             */
/*                                                             */
/* Benchmark                                                   */
/* =========                                                   */
/*                                                             */
/* The test was run on a Intel Celeron (Tualatin core)         */
/* computer with 256 kbyte cache memory and 512 Mbyte SDRAM    */
/* primary memory. The operating system was Red Hat 9 Linux.   */
/* Compiler tested was GNU GCC 3.22 (gcc) for Linux.           */
/*                                                             */
/* Numbers tested:                                             */
/* ---------------                                             */
/*                                                             */
/* n = 3; m = 11                                               */
/*                                                             */
/* Results:                                                    */
/* --------                                                    */
/*                                                             */
/* Computer:         CPU-time (s):  Compiler options:          */
/* =========================================================== */
/*                                                             */
/* Celeron 1400 MHz       6.42      gcc -O3                    */
/*                                                             */
/***************************************************************/

/********************************************************************************/
/*                                                                              */
/* Notice                                                                       */
/* ======                                                                       */
/*                                                                              */
/* I make no warranties that this program is (1) free of errors, (2) consistent */
/* with any standard merchantability, or (3) meeting the requirements of a      */
/* particular application. This software shall not, partly or as a whole,       */
/* participate in a process, whose outcome can result in injury to a person or  */
/* loss of property. It is solely designed for analytical work. Permission to   */
/* use, copy and distribute is hereby granted without fee, providing that the   */
/* header above including this notice appears in all copies.                    */
/*                                                                              */
/*                                                            Stefan Spaennare  */
/*                                                                              */
/********************************************************************************/


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

double timetic = (double) (CLOCKS_PER_SEC);

int s; 

int ack(int n, int m)
{
   s++;

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

} /* ack */


int main(int argc, char **argv)
{

   int m,n,r;

   clock_t time1, timediff;

   if (argc != 3) {
     printf("Usage: %s ni mi\n",argv[0]);
     exit(0);
   } /* if */

   n = atoi(argv[1]);
   m = atoi(argv[2]);

   s = 0;

   time1 = clock();

   r = ack(n, m);

   timediff = clock() - time1;

   printf("\n");
   printf("Recursion depth: %10d\n",s);
   printf("Ackermann:       %10d\n",r);
   printf("CPU-time:          %8.2f s\n",(double)(timediff)/timetic);
   printf("\n");

} /* End */
