From 8eb27d712f6b0a80880459b4a1531b3512dce8a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20=C5=81ukaszewicz?= <26778292+5n0wwh1t3@users.noreply.github.com> Date: Sun, 22 Nov 2020 18:08:36 +0000 Subject: [PATCH] Added Ackermann function (#794) * Ackermann added. * Ackermann updated --- src/main/java/com/others/Ackermann.java | 34 +++++++++++++++++++++ src/test/java/com/others/AckermannTest.java | 19 ++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/com/others/Ackermann.java create mode 100644 src/test/java/com/others/AckermannTest.java 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)); + } + + + +}