-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.java
More file actions
127 lines (116 loc) · 2.25 KB
/
factorial.java
File metadata and controls
127 lines (116 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Arrays;
public class factorial {
int[] one= {1,3,4,7,9};
int[] two= {2,3,4,6,8};
public static int factorial1(int n)
{
if(n==1)
{
return n;
}
else
{
return(n*factorial1(n-1));
}
}
//if I change the * to a + it will add up all the numbers under and equal to the number u inputted
public static int factorial2(int n)
{
if(n==1)
{
return n;
}
else
{
return(n+factorial2(n-1));
}
}
public static int power(int base, int exp)
{
if(exp==1)
{
return base;
}
else
{
return base*power(base,exp-1);
}
}
/**public static int[] merge(int[] a, int[]b)
{
int[] temp = new int[a.length+b.length];
int positiona = 0;
int positionb = 0;
int positiontemp = 0;
while(positiona < a.length && positionb < b.length)
{
if(a[positiona]<=b[positionb])
{
positiontemp= a[positiona];
positiona++;
positiontemp++;
}
else
{
temp[positiontemp]=b[positionb];
positionb++;
positiontemp++;
}
while(positiona<a.length)
{
temp[positiontemp]= a[positiona];
positiona++;
positiontemp++;
}
while(positiona<b.length)
{
temp[positiontemp]= b[positiona];
positionb++;
positiontemp++;
}
return temp;
}
}
/**
* power10 will be power 5 power 5
* the first power 5 will split into power 2 and power 3
* the second will do the same thing
* power 2 will split into power 1 and power 1
* power3 will split into pow 2 and pow 1\
* pow 2 will split into pow 1 and pow 1
* @param base
* @param exp
* @return
*/
public static int power1(int base, int exp)
{
if(exp==1)
{
return base;
}
else
{
return power1(base, exp/2) *power1(base, exp-exp/2);
}
}
public static int[] sort(int[] a)
{
if(a.length==1)
{
return a;
}
else
{
return merge(sort(Arrays.copyOfRange(a, 0, a.length/2)), sort(a,a.length/2,a.length));
//the first index is included and the second is excluded
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(factorial1(6));
System.out.println(factorial2(6));
System.out.println(power(2,10));
System.out.println(power1(2,10));
//System.out.println(Arrays.toString(merge(one,two)));
}
}