Added Ackermann function (#794)

* Ackermann added.

* Ackermann updated
This commit is contained in:
Krzysztof Łukaszewicz 2020-11-22 18:08:36 +00:00 committed by GitHub
parent 1012ff74f3
commit 8eb27d712f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -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));
}
}

View File

@ -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));
}
}