-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetExponent.java
More file actions
43 lines (40 loc) · 1.04 KB
/
Copy pathGetExponent.java
File metadata and controls
43 lines (40 loc) · 1.04 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
public class GetExponent {
public static void main(String[] args) {
System.out.println(getExponent(162, 3));
System.out.println(getExponent(27, 3));
System.out.println(getExponent(28, 3));
System.out.println(getExponent(280, 7));
System.out.println(getExponent(-250, 5));
System.out.println(getExponent(18, 1));
System.out.println(getExponent(128, 4));
System.out.println(getExponent(1, 5));
System.out.println(getExponent(3, 5));
}
static int getExponent1(int n, int p) {
if(p <= 1) return -1;
n = (n < 0)? -n : n;
int exp = 0;
for (int x = 0; Math.pow(p, x) <= n; x++) {
if(n % Math.pow(p,x) == 0)
exp = x;
}
return exp;
}
static int getExponent(int n, int p) {
if(p <= 1) return -1;
n = (n < 0)? -n : n;
int power = 0, pMultiple = 1;
while(pMultiple < n) {
if(n % pMultiple == 0) {
pMultiple *= p;
power++;
}
else {
return --power;
}
}
if(pMultiple == n)
return power;
return --power;
}
}