ack.c

/*
 * Computing Ackermann's function
 *
 * Matt Bishop, ECS 36A
 *
 * April 29, 2024
 *	 -- original versionb
 */
#include <stdio.h>

/*
 * macros for ease of changing
 */
#define	M 3
#define N 4

/*
 * the function
 */
int ack(int m, int n)
{
	/* base case: n = 0 */
	if (m == 0)
		return(n + 1);
	
	/* recursice part */
	if (n == 0)
		return(ack(m - 1, 1));
	return(ack(m - 1, ack(m, n - 1)));
}

/*
 * the program
 */
int main(void)
{
	/* do the computation of A(M, N) */
	/* and print the result          */
	printf("ack(%d, %d) = %d\n", M, N, ack(M, N));

	/* done! */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh