diff --git a/src/main/java/com/others/Ackermann.java b/src/main/java/com/others/Ackermann.java new file mode 100644 index 00000000..a06cfaa6 --- /dev/null +++ b/src/main/java/com/others/Ackermann.java @@ -0,0 +1,34 @@ +package src.main.java.com.others; + + +public class Ackermann { + + + /** + * Ackermann function - simplest and earliest-discovered examples of a total computable function + * that is not primitive recursive. + * + * Defined only for NONNEGATIVE integers !!! + * + * Time complexity is super-exponential. O(n(^)) + * Any input m higher tahn (3,3) will result in StackOverflow + * @param m + * @param n + * @return + * + * + */ + public long Ack(long m, long n) { + + if (m == 0) + return n + 1; + + if (n == 0) + return Ack(m - 1, 1); + + return Ack(m - 1, Ack(m, n - 1)); + } + +} + + diff --git a/src/test/java/com/others/AckermannTest.java b/src/test/java/com/others/AckermannTest.java new file mode 100644 index 00000000..c45b5542 --- /dev/null +++ b/src/test/java/com/others/AckermannTest.java @@ -0,0 +1,19 @@ +package src.test.java.com.others; + +import src.main.java.com.others.Ackermann; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class AckermannTest { + + @Test + public void testAckermann() { + Ackermann ackTest = new Ackermann(); + assertEquals("Error", 1, ackTest.Ack(0, 0)); + assertEquals("Error", 3, ackTest.Ack(1, 1)); + assertEquals("Error", 7, ackTest.Ack(2, 2)); + } + + + +}