Skip to content

Added Ackermann function #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/com/others/Ackermann.java
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions src/test/java/com/others/AckermannTest.java
Original file line number Diff line number Diff line change
@@ -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));
}



}