Skip to content

Commit 7c6c007

Browse files
author
Sherali Obidov
committed
added missing package statements
1 parent 6b4890a commit 7c6c007

3 files changed

Lines changed: 53 additions & 48 deletions

File tree

‎src/algo/recursion/Ackermann.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/*
1+
package algo.recursion;
2+
/**
23
* the Ackermann function is one of the simplest and earliest-discovered examples of a total computable function that is not primitive recursive.
34
* All primitive recursive functions are total and computable, but the Ackermann function illustrates that not all total computable functions are primitive recursive.
45
* Ackermann Function is given by :
@@ -12,12 +13,13 @@
1213

1314
// This method may give Stack Overflow Exception
1415
public class Ackermann {
15-
16-
private static BigInteger ackermann(BigInteger m, BigInteger n) {
17-
return m.equals(BigInteger.ZERO) ? n.add(BigInteger.ONE) : ackermann(m.subtract(BigInteger.ONE), n.equals(BigInteger.ZERO) ? BigInteger.ONE : ackermann(m, n.subtract(BigInteger.ONE)));
18-
}
1916

20-
public static void main(String[] args) {
21-
System.out.println(ackermann(new BigInteger("2"), new BigInteger("2")));
22-
}
17+
private static BigInteger ackermann(BigInteger m, BigInteger n) {
18+
return m.equals(BigInteger.ZERO) ? n.add(BigInteger.ONE) :
19+
ackermann(m.subtract(BigInteger.ONE), n.equals(BigInteger.ZERO) ? BigInteger.ONE : ackermann(m, n.subtract(BigInteger.ONE)));
20+
}
21+
22+
public static void main(String[] args) {
23+
System.out.println(ackermann(new BigInteger("2"), new BigInteger("2")));
24+
}
2325
}
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package algo.recursion;
2+
13
/*
24
* Ackermann Function is given by :
35
* A (m , n) = n + 1, if m = 0
@@ -11,31 +13,30 @@
1113
// This method overcomes the problem of Stack Overflow Exception
1214
public class AckermannNonRecursive {
1315

14-
private static BigInteger ackermann(BigInteger m, BigInteger n) {
15-
16-
Stack<BigInteger> ack = new Stack<BigInteger>(); // Creating a stack to store the values
17-
ack.push(m);
18-
19-
while (!ack.isEmpty()) { // Looping while the stack is not empty
20-
m = ack.pop(); // Assigning m the value of top element of the stack
21-
if (m.equals(BigInteger.ZERO)) // If m = 0, n = n + 1
22-
n = n.add(BigInteger.ONE);
23-
else if (n.equals(BigInteger.ZERO)) { // If n = 0, push m - 1 in the stack and update the value of n to 1
24-
BigInteger temp = m.subtract(BigInteger.ONE);
25-
ack.push(temp);
26-
n = BigInteger.ONE;
27-
}
28-
else { // If n > 0 and m > 0, push m - 1 and m in the stack respectively and subtract 1 from n
29-
BigInteger temp = m.subtract(BigInteger.ONE);
30-
ack.push(temp);
31-
ack.push(m);
32-
n = n.subtract(BigInteger.ONE);
33-
}
34-
}
35-
return n;
36-
}
16+
private static BigInteger ackermann(BigInteger m, BigInteger n) {
17+
18+
Stack<BigInteger> ack = new Stack<BigInteger>(); // Creating a stack to store the values
19+
ack.push(m);
20+
21+
while (!ack.isEmpty()) { // Looping while the stack is not empty
22+
m = ack.pop(); // Assigning m the value of top element of the stack
23+
if (m.equals(BigInteger.ZERO)) // If m = 0, n = n + 1
24+
n = n.add(BigInteger.ONE);
25+
else if (n.equals(BigInteger.ZERO)) { // If n = 0, push m - 1 in the stack and update the value of n to 1
26+
BigInteger temp = m.subtract(BigInteger.ONE);
27+
ack.push(temp);
28+
n = BigInteger.ONE;
29+
} else { // If n > 0 and m > 0, push m - 1 and m in the stack respectively and subtract 1 from n
30+
BigInteger temp = m.subtract(BigInteger.ONE);
31+
ack.push(temp);
32+
ack.push(m);
33+
n = n.subtract(BigInteger.ONE);
34+
}
35+
}
36+
return n;
37+
}
3738

38-
public static void main(String[] args) {
39-
System.out.println(ackermann(new BigInteger("3"), new BigInteger("4")));
40-
}
39+
public static void main(String[] args) {
40+
System.out.println(ackermann(new BigInteger("3"), new BigInteger("4")));
41+
}
4142
}

‎src/algo/recursion/TowerOfHanoi.java‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/*
1+
package algo.recursion;
2+
3+
/**
24
* Tower of Hanoi is a mathematical puzzle where we have three rods and n disks.
35
* The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
46
* 1) Only one disk can be moved at a time.
@@ -48,18 +50,18 @@
4850
*/
4951

5052
public class TowerOfHanoi {
51-
public static void towerOfHanoi(int top, char start, char via, char end) {
52-
if (top == 1)
53-
System.out.println(start + " --> " + end); // If one disk is left, move it to last rod
54-
else {
55-
towerOfHanoi(top - 1, start, end, via); // Shifting n - 1 disks from A to B with the help of C
56-
System.out.println(start + " --> " + end); // Shifting last disk from A to C
57-
towerOfHanoi(top - 1, via, start, end); // Shifting n - 1 disks from B to C with the help of A
58-
}
59-
}
53+
public static void towerOfHanoi(int top, char start, char via, char end) {
54+
if (top == 1)
55+
System.out.println(start + " --> " + end); // If one disk is left, move it to last rod
56+
else {
57+
towerOfHanoi(top - 1, start, end, via); // Shifting n - 1 disks from A to B with the help of C
58+
System.out.println(start + " --> " + end); // Shifting last disk from A to C
59+
towerOfHanoi(top - 1, via, start, end); // Shifting n - 1 disks from B to C with the help of A
60+
}
61+
}
6062

61-
public static void main(String[] args) {
62-
int blocks = 5; // Number of blocks in the tower
63-
towerOfHanoi(blocks, 'A', 'B', 'C'); // Moving all blocks from A to C
64-
}
63+
public static void main(String[] args) {
64+
int blocks = 5; // Number of blocks in the tower
65+
towerOfHanoi(blocks, 'A', 'B', 'C'); // Moving all blocks from A to C
66+
}
6567
}

0 commit comments

Comments
 (0)