-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.cpp
51 lines (42 loc) · 974 Bytes
/
recursion.cpp
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
#include "iostream"
#include "map"
#include "string"
#define REP(i, n) for(int i = 0; i < n; i++)
#define N 30
using namespace std;
int factorial(int x) {
if (x == 0 || x == 1) return 1;
return x * factorial(x - 1);
}
int gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
// test function that computes f(N) = (N/2) + f(N/2) in a top down manner
int computed[N];
int rel(int x) {
if (x == 0) return 0;
if (computed[x]) return computed[x];
computed[x] = (x/2) + rel(x/2);
return computed[x];
}
int computedSums[N];
int sumTillN(int n) {
cout << "Called : " << n << '\n';
if (n == 0) return 0;
if (computedSums[n]) return computedSums[n];
computedSums[n] = n + sumTillN(n - 1);
return computedSums[n];
}
int main()
{
cout << factorial(0) << '\n';
cout << gcd(60, 12) << '\n';
cout << rel(5) << '\n';
for(int i = 0; i < N; i++) computedSums[i] = 0;
cout << sumTillN(5);
cout << '\n';
cout << sumTillN(7);
cout << '\n';
return 0;
}