Created
August 7, 2014 17:32
-
-
Save TheBB/26c3b38ac24333c16913 to your computer and use it in GitHub Desktop.
Ackermann function
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
#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