Skip to content

Instantly share code, notes, and snippets.

@ungalcrys
Created June 24, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ungalcrys/9081cf5fa88717767a2d to your computer and use it in GitHub Desktop.
Save ungalcrys/9081cf5fa88717767a2d to your computer and use it in GitHub Desktop.
Simple loop for Ackermann function
import java.util.ArrayList;
public class LiniarAckermann {
static ArrayList<Long> mList = new ArrayList<Long>();
public static long ackermann(long m, long n) {
while (true) {
if (m == 0) {
n += 1;
if (mList.isEmpty()) {
return n;
} else {
int index = mList.size() - 1;
m = mList.get(index);
mList.remove(index);
}
} else if (n == 0) {
m -= 1;
n = 1;
} else {
mList.add(m - 1);
n -= 1;
}
}
}
public static void main(String[] args) {
System.out.println(ackermann(4, 1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment