-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP18_Ackermann Function.cpp
More file actions
94 lines (68 loc) · 2.68 KB
/
P18_Ackermann Function.cpp
File metadata and controls
94 lines (68 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Primitive Funtion is the function that has base case and be computed in finite number of cycles, Function that can be obtained by a successor, zero and projection.
// Ackermann is Total recursive but not primitive recursive Function
int ack(int m, int n)
{
if (m == 0)
return n + 1;
else if ((m > 0) && (n == 0))
return ack(m - 1, 1);
else if ((m > 0) && (n > 0))
return ack(m - 1, ack(m, n - 1));
}
//vector<vector<int>>ackMemoization(1000, vector<int>(1000, -1));
ll modifiedAck(ll m, ll n)
{
if (m == 0)
return n + 1;
if (m == 1)
return n + 2;
if (m == 2)
return 2 * n + 3;
if (m > 2 && n == 0)
return modifiedAck(m - 1, 1); //return 1 << ((n + 3) - 3); for m = 3
if ((m > 2) && (n > 0))
return modifiedAck(m - 1, modifiedAck(m, n - 1));
/*
*******Memoization*******
if (ackMemoization[m][n] != -1)
return ackMemoization[m][n];
if (m == 0)
ackMemoization[m][n] = n + 1;
else if (m > 0 && n == 0)
ackMemoization[m][n] = modifiedAck(m - 1, 1);
else
ackMemoization[m][n] = modifiedAck(m - 1, modifiedAck(m, n - 1));
*/
}
int main()
{
ios::sync_with_stdio(false), cin.tie(), cout.tie();
//This Function grows very quickly:
cout << "Using Recursion and basic Ackermann:\n";
cout << "m = 0, n = 2 Ack = " << ack(0, 2) << endl;
cout << "m = 1, n = 2 Ack = " << ack(1, 2) << endl;
cout << "m = 2, n = 2 Ack = " << ack(2, 2) << endl;
cout << "m = 3, n = 2 Ack = " << ack(3, 2) << endl;
cout << "Cannot compute ack(4, 2) using simple recursion as it's about 2*10^19728 \n";
cout << endl;
cout << "m = 0, n = 1 Ack = " << ack(0, 1) << endl;
cout << "m = 1, n = 1 Ack = " << ack(1, 1) << endl;
cout << "m = 2, n = 1 Ack = " << ack(2, 1) << endl;
cout << "m = 3, n = 1 Ack = " << ack(3, 1) << endl;
cout << "Cannot compute ack(4, 1) using simple recursion\n";
//So, it's an exhaustive function that is hard to be computed
cout << "------------------------------------------------------------\nUsing Modified Ackermann (Direct Computation)\n\n";
cout << "m = 0, n = 1 Ack = " << modifiedAck(0, 1) << endl;
cout << "m = 1, n = 1 Ack = " << modifiedAck(1, 1) << endl;
cout << "m = 2, n = 1 Ack = " << modifiedAck(2, 1) << endl;
cout << "m = 3, n = 1 Ack = " << modifiedAck(3, 1) << endl;
cout << "m = 4, n = 1 Ack = " << modifiedAck(4, 1) << endl;
// We can optimize more by using Memoization as shown in modifiedAck function Comment Section but has a problem of bad space complexity
cout << endl << "-------------Sample Inputs------------\n";
cout << "m = 3, n = 3 Ack = " << modifiedAck(3, 3) << endl;
cout << "m = 4, n = 1 Ack = " << modifiedAck(4, 1) << endl;
return 0;
}