Skip to content

Commit 8eb27d7

Browse files
Added Ackermann function (TheAlgorithms#794)
* Ackermann added. * Ackermann updated
1 parent 1012ff7 commit 8eb27d7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package src.main.java.com.others;
2+
3+
4+
public class Ackermann {
5+
6+
7+
/**
8+
* Ackermann function - simplest and earliest-discovered examples of a total computable function
9+
* that is not primitive recursive.
10+
*
11+
* Defined only for NONNEGATIVE integers !!!
12+
*
13+
* Time complexity is super-exponential. O(n(^))
14+
* Any input m higher tahn (3,3) will result in StackOverflow
15+
* @param m
16+
* @param n
17+
* @return
18+
*
19+
*
20+
*/
21+
public long Ack(long m, long n) {
22+
23+
if (m == 0)
24+
return n + 1;
25+
26+
if (n == 0)
27+
return Ack(m - 1, 1);
28+
29+
return Ack(m - 1, Ack(m, n - 1));
30+
}
31+
32+
}
33+
34+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package src.test.java.com.others;
2+
3+
import src.main.java.com.others.Ackermann;
4+
import static org.junit.Assert.assertEquals;
5+
import org.junit.Test;
6+
7+
public class AckermannTest {
8+
9+
@Test
10+
public void testAckermann() {
11+
Ackermann ackTest = new Ackermann();
12+
assertEquals("Error", 1, ackTest.Ack(0, 0));
13+
assertEquals("Error", 3, ackTest.Ack(1, 1));
14+
assertEquals("Error", 7, ackTest.Ack(2, 2));
15+
}
16+
17+
18+
19+
}

0 commit comments

Comments
 (0)