parent
1012ff74f3
commit
8eb27d712f
34
src/main/java/com/others/Ackermann.java
Normal file
34
src/main/java/com/others/Ackermann.java
Normal 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
19
src/test/java/com/others/AckermannTest.java
Normal file
19
src/test/java/com/others/AckermannTest.java
Normal 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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user