-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPower_Function.java
More file actions
72 lines (60 loc) · 1.66 KB
/
Copy pathPower_Function.java
File metadata and controls
72 lines (60 loc) · 1.66 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
public class Power_Function {
// Approach 1
public static double Power_Recursive(int x,int n){
if(n==0){
return 1;
}
if(n==1){
return x;
}
if(x==0){
return 0;
}
// Handle negative exponents: x^-n = 1 / x^n
if (n < 0) {
// We pass -n to make the exponent positive for the calculation,
// then divide 1 by the result.
return 1.0 / Power_Recursive(x, -n);
}
double Half_Power=Power_Recursive(x,n/2);
if(n%2==0){
// When Power is even
return Half_Power*Half_Power;
}
else{
// When Power is odd
return x * Half_Power * Half_Power;
}
}
// Approach 2
public static double Power_Iterative(double x,int n){
if(x==1 || n==0){
return 1.0;
}
if(n==1){
return x;
}
long N=n; // To prevent Overflow
if(N<0){ // to handle Negative Powers
x = 1.0 / x ;
N = -N;
}
double ans = 1.0;
double curr_product = x;
while(N>0){
// If the Power is odd at any time , we will multiply x with our ans
if((N & 1) == 1){ // N % 2 == 1
ans = ans * curr_product;
}
// Square the base for the next power of 2
curr_product = curr_product * curr_product;
N = N >> 1; // N /= 2
}
return ans;
}
public static void main(String[] args){
int x=2;
int n=10;
System.out.println(Power_Iterative(x,n));
}
}