Last active
April 12, 2020 12:30
-
-
Save retokromer/9e686c1527c29b98f057116cb3d614a8 to your computer and use it in GitHub Desktop.
Ackermann function computed recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2020 by Reto Kromer | |
* | |
* Ackermann function: | |
* | |
* A(0,n) = n+1 | |
* A(m,0) = A(m-1,1) if m > 0 | |
* A(m,n) = A(m-1,A(m,n-1)) if m > 0 and n > 0 | |
* | |
* where m and n are non-negative integers | |
* | |
* computed recursively. | |
* | |
* This C program is released under a 3-Clause BSD License and is provided | |
* "as is" without warranty or support of any kind. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
unsigned int A(unsigned int m, unsigned int n) { | |
return m == 0 ? n+1 : ( n == 0 ? A(m-1,1) : A(m-1,A(m,n-1)) ); | |
} | |
int main(int argc, char* argv[]) { | |
unsigned int m, n; | |
if (argc == 3) { | |
m = atoi(argv[1]); | |
n = atoi(argv[2]); | |
} else { | |
printf("%s", "m = "); | |
scanf("%d", &m); | |
printf("%s", "n = "); | |
scanf("%d", &n); | |
} | |
printf("A(%d,%d) = %d\n", m, n, A(m,n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment