Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created March 24, 2017 14:55
Show Gist options
  • Save joennlae/2d762a255b9a9cc36ebeeb1fdff30b69 to your computer and use it in GitHub Desktop.
Save joennlae/2d762a255b9a9cc36ebeeb1fdff30b69 to your computer and use it in GitHub Desktop.
package u4a2;
import u4a1.Stack;
public class IterativeAckermann {
/**
* Stack-based iterative implementation of the Ackermann function.
*
* @param n parameter n
* @param m parameter m
* @return Ackermann(n,m)
*/
public int A(int n, int m)
{
Stack s = new Stack(10);
s.push(n);
s.push(m);
int nS = 0;
int mS = 0;
while(s.size()>1){
System.out.println(s.toString());
mS = s.pop();
nS = s.pop();
if(nS == 0){
s.push(mS+1);
}
else if(nS>0 &&mS == 0){
s.push(nS-1);
s.push(1);
}
else if(nS >0 &&mS > 0){
s.push(nS-1);
s.push(nS);
s.push(mS-1);
}
}
return mS +1;
}
}
package u4a1;
import java.util.EmptyStackException;
/**
* Dynamically growing stack.
*/
public class Stack {
private int[] buffer;
private int size;
/**
* Creates a new stack
*
* @param capacity the initial capacity of the stack
*/
public Stack(int capacity) throws IllegalArgumentException
{
if(capacity==0) throw new IllegalArgumentException("initial stack size 0");
buffer = new int[capacity];
size = 0;
}
/**
* Converts stack to a string representation.
*
* @return A ", "-separated list of the numbers, enclosed in square brackets. Bottom numbers come first.
*/
public String toString()
{
StringBuffer output = new StringBuffer();
output.append("[");
for(int i= 0; i<size; i++){
if (i != 0)
output.append(", ");
output.append(Integer.toString(buffer[i]));
}
output.append("]");
return output.toString();
}
/**
* Doubles the capacity of the stack.
*
* Copies all objects to a new buffer of doubled size.
*/
private void grow()
{
if(size==buffer.length){
buffer = java.util.Arrays.copyOf(buffer,buffer.length*2);
}
}
/**
* Pushes a number onto the top of this stack.
*
* Grows the stack if necessary.
*
* @param number the number to be pushed onto this stack.
*/
public void push(int number)
{
grow();
buffer[size++] = number;
}
/**
* Removes the number at the top of this stack and returns it as the value of this function.
*
* @return The number at the top of this stack
* @throws EmptyStackException if this stack is empty
*/
public int pop() throws EmptyStackException
{
if(size==0) throw new EmptyStackException();
int res = buffer[--size];
buffer[size] = 0;
return res;
}
/**
* Looks at the number at the top of this stack without removing it from the stack.
*
* @return the number at the top of this stack
* @throws EmptyStackException if this stack is empty
*/
public int peek() throws EmptyStackException
{
if(size==0) throw new EmptyStackException();
int res = buffer[--size];
size++;
return res;
}
/**
* Tests if this stack is empty.
*
* @return true if and only if this stack contains no items; false otherwise.
*/
public boolean empty()
{
return (size==0);
}
/**
* Get the size of this stack.
*
* @return the current number of items on this stack
*/
public int size()
{
return size;
}
/**
* Get the capacity of this stack.
*
* @return the maximum number of items this stack can hold without having to grow
*/
public int capacity()
{
return buffer.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment