Skip to content

Instantly share code, notes, and snippets.

@TheBB
Created August 7, 2014 17:32
Show Gist options
  • Save TheBB/26c3b38ac24333c16913 to your computer and use it in GitHub Desktop.
Save TheBB/26c3b38ac24333c16913 to your computer and use it in GitHub Desktop.
Ackermann function
#include <iostream>
#include <stack>
#include <cstdlib>
typedef unsigned long long ull;
ull ack(ull m, ull n)
{
std::stack<ull> st;
st.push(m);
while (!st.empty())
{
if (st.top() == 0)
{
st.pop();
n++;
}
else if (n == 0)
{
m = st.top();
st.pop();
st.push(m - 1);
n = 1;
}
else
{
m = st.top();
st.pop();
st.push(m - 1);
st.push(m);
n--;
}
}
return n;
}
int main(int argc, char **argv)
{
ull m = std::atoll(argv[1]);
ull n = std::atoll(argv[2]);
std::cout << "ack(" << m << ", " << n << ") = " << ack(m,n) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment