-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.java
More file actions
95 lines (90 loc) · 2.25 KB
/
recursion.java
File metadata and controls
95 lines (90 loc) · 2.25 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
95
import java.util.*;
public class recursion {
public static void printdecen(int n){
if (n==0){
return;
}
System.out.println(n);
printdecen(n-1);
}
public static void printascen(int m){
if (m==6){
return;
}
System.out.println(m);
printascen(m+1);
}
public static void sumOfNum(int a,int n1,int sum){
if (a==n1+1){
System.out.println(sum);
return;
}
sum=sum+a;
System.out.print(sum+" ");
sumOfNum(a+1,n1,sum);
}
public static void facOfNum(int a,int n1,int fac){
if (a==n1+1){
System.out.println(fac);
return;
}
fac=fac*a;
System.out.print(fac+" ");
facOfNum(a+1,n1,fac);
}
public static void fibonacci(int x,int y,int n2){
if(n2==0){
return;
}
int c=x+y;
System.out.print(c+" ");
fibonacci(y, c, n2-1);
}
public static int calPower(int c,int d){
if (d==0){
return 1;
}
if(c==0){
return 0;
}
int cpowdm1=calPower(c, d-1);
int cpowd=c*cpowdm1;
return cpowd;
}
public static int calPower2(int c,int d){
if (d==0){
return 1;
}
if(c==0){
return 0;
}
if(d%2==0){
return calPower2(c, d/2)* calPower(c, d/2);
}else{
return calPower(c, d/2)* calPower(c, d/2)* c;
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n1=sc.nextInt();
int a=1;
int sum=0 ;
int fac=1;
sumOfNum(a,n1,sum);
facOfNum(a,n1,fac);
int x=0,y=1,n2=7;
System.out.print(x+" "+y+" ");
fibonacci(x, y, n2-2);
System.out.println(" ");
int n=5;
printdecen(n);
int m=1;
printascen(m);
int c=2,d=5;
int ans=calPower(c, d);
System.out.println(ans);//height is n
int ans2=calPower2(c, d);
System.out.println(ans2);//height is logn
}
}
//print x^n (stack height=n)???