forked from Anuj-Kumar-Sharma/DS-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
56 lines (43 loc) · 1013 Bytes
/
MainClass.java
File metadata and controls
56 lines (43 loc) · 1013 Bytes
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
package recursion;
public class MainClass {
static int stepCount = 0;
public static void main(String[] args) {
// System.out.println(sum(15));
// System.out.println(pow(3, 10000));
// System.out.println("steps " + stepCount);
//
// stepCount = 0;
// System.out.println(fastPow(3, 10000));
// System.out.println("steps " + stepCount);
System.out.println(path(200, 1));
}
static int sum(int n) {
if(n == 1) {
return 1;
}
return n + sum(n-1);
}
static int pow(int a, int b) {
stepCount++;
if(b == 0) {
return 1;
}
return a * pow(a, b-1);
}
static int fastPow(int a, int b) {
System.out.println(b);
stepCount++;
if(b == 0) {
return 1;
}
if(b%2 ==0) {
return fastPow(a*a, b/2);
}
return a*fastPow(a, b-1);
}
//recursive method to find total no.paths to travel from top left corner to bottom right corner in a n*m grid
static int path(int n, int m) {
if(n == 1 || m == 1) return 1;
return path(n, m-1) + path(m, n-1);
}
}