Skip to content

Commit

Permalink
Create Pow(x,n).java
Browse files Browse the repository at this point in the history
  • Loading branch information
RJSIN147 authored and x0lg0n committed Nov 1, 2024
1 parent 9a76e6d commit 6d252d3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Java/Pow(x,n).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Main {
public double myPow(double x, int n) {
if (n == 0) {
return 1;
}

long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}

double result = 1;
double currentProduct = x;

while (N > 0) {
if (N % 2 == 1) {
result *= currentProduct;
}
currentProduct *= currentProduct;
N /= 2;
}

return result;
}
}

0 comments on commit 6d252d3

Please sign in to comment.