-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionFunction.cpp
More file actions
65 lines (57 loc) · 1.37 KB
/
Copy pathRecursionFunction.cpp
File metadata and controls
65 lines (57 loc) · 1.37 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
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int sumOfDigits(int n){
if(n<=0) return 0;
return n%10 + sumOfDigits(n/10);
}
void printNnumbers(int n){
//Print 1 to N
if(n <1) return;
printNnumbers(n-1);
std::cout << n<<" ";
}
int countDigits(int n){
// Count digits in number
if(n<10) return 1;
return 1+countDigits(n/10);
}
int digitalRoot(int n){ //Eg. DigitalRoot(191)=1+9+1=>11=>1+1=>2
if(n <10) return n;
return digitalRoot(n%10 + digitalRoot(n/10));
}
long long fibonacci(int n){ //nth fibonacci number
if(n==1 | n==0) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int rodCutting(int n, int a, int b, int c){
//
if(n == 0)
return 0;
if(n < 0)
return -1;
int res = std::max(std::max(rodCutting(n-a, a, b, c), rodCutting(n-b, a, b, c)),
rodCutting(n-c, a, b, c));
if(res== -1)
return -1;
return 1+res;
}
void subset(string s, string curr, int index){
// generates all subsets and print from string
if(s.length() == index){
cout << curr <<", ";
return;
}
subset(s,curr,index+1);
subset(s,curr+s[index],index+1);
}
int subsetSum(int s[], int n, int sum){
// return number of subets of set having sum = sum | Theta(2^n)
if(n ==0)return (sum == 0)?1:0;
return subsetSum(s,n-1,sum) +
subsetSum(s,n-1,sum-s[n-1]);
}
int main() {
printNnumbers(6);
}