Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 1, 2025 12:50
Show Gist options
  • Select an option

  • Save juanfal/79ca4e73649a5a0b3fe3fd82923f3e4e to your computer and use it in GitHub Desktop.

Select an option

Save juanfal/79ca4e73649a5a0b3fe3fd82923f3e4e to your computer and use it in GitHub Desktop.
Ackermann, heavily recursive function
// ackerman.cpp
// juanfc 2025-11-01
// ackermann(4, 0) -> 13
// ackermann(4, 1) -> 65533
// ackermann(4, 2) -> … 19,729 digits …
//
// https://gist.github.com/juanfal/79ca4e73649a5a0b3fe3fd82923f3e4e
#include <iostream>
using namespace std;
int main()
{
int ackermann(int, int);
int m, n;
do {
cout << "Introducir m y n: ";
cin >> m >> n;
cout << "ackermann(" << m << "," << n << "): " << ackermann(m, n) << endl;
} while (m != 0 or n != 0);
return 0;
}
int ackermann(int m, int n)
{
if (m == 0)
return n+1;
else if (n == 0)
return ackermann(m-1, 1);
else
return ackermann(m-1, ackermann(m, n-1));
}
// ackermann.cpp
// juanfc 2025-11-01
// ackermann(4, 0) -> 13
// ackermann(4, 1) -> 65533
// ackermann(4, 2) -> … 19,729 digits …
//
// https://gist.github.com/juanfal/79ca4e73649a5a0b3fe3fd82923f3e4e
#include <iostream>
using namespace std;
int main()
{
int ackermann(int, int);
int m, n;
do {
cout << "Introducir m y n: ";
cin >> m >> n;
cout << "ackermann(" << m << "," << n << "): " << ackermann(m, n) << endl;
} while (m != 0 or n != 0);
return 0;
}
int ackermann(int m, int n)
{
if (m == 0)
return n+1;
else if (n == 0)
return ackermann(m-1, 1);
else
return ackermann(m-1, ackermann(m, n-1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment