-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastExponentiation.java
55 lines (51 loc) · 1.31 KB
/
FastExponentiation.java
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
/********************************************************
x=2, n=13
2*2^n-1 ---> 2*2^12--->
(2*2)^12/2 ---> (4)^6 --->
(4*4)^6/2 ---> 16^3 --->
16*16^2 ---> 16^2= 256 ---> 256*16=4096 --->4096*2=8192
********************************************************/
public class FastExponentiation {
private static int rcount=0;
private static int icount;
public static double fastPotRek(double x, int n){
rcount++;
if (n==0){
return 1;
}
if (n==1){
return x;
}
if (n%2==0){
return fastPotRek(x*x, n/2);
}
else{
x=x*fastPotRek(x, n-1);
return x;
}
}
public static double fastPotIter(double x, int n){
double temp=1;
icount=0;
while(n>1){
icount++;
if (n%2==0){
x=x*x;
n=n/2;
}
else{
n=n-1;
temp=temp*x;
}
}
x=x*temp;
return x;
}
public static void main(String[] args) {
double a=7;
int b=57;
double c= fastPotIter(a, b);
double d= fastPotRek(a,b);
System.out.println(c+"\n"+d+"\n"+icount+"\n"+rcount);
}
}