Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8eb27d7

Browse files
Added Ackermann function (TheAlgorithms#794)
* Ackermann added.* Ackermann updated
1 parent1012ff7 commit8eb27d7

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+
packagesrc.main.java.com.others;
2+
3+
4+
publicclassAckermann {
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+
publiclongAck(longm,longn) {
22+
23+
if (m ==0)
24+
returnn +1;
25+
26+
if (n ==0)
27+
returnAck(m -1,1);
28+
29+
returnAck(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+
packagesrc.test.java.com.others;
2+
3+
importsrc.main.java.com.others.Ackermann;
4+
importstaticorg.junit.Assert.assertEquals;
5+
importorg.junit.Test;
6+
7+
publicclassAckermannTest {
8+
9+
@Test
10+
publicvoidtestAckermann() {
11+
AckermannackTest =newAckermann();
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)

[8]ページ先頭

©2009-2025 Movatter.jp