StackOverFlow Error For Problem Number 50 in Leetcode - java

For the below Input I am getting a StackOverflow error. Can u guys help me with an explanation and how to solve this problem in my code.
System.out.println(myPow(0.00001,2147483647));
public static double myPow(double x, int n) {
return helperPow(x,n,1);
}
private static double helperPow(double x, int n,double d) {
if(n == 0) {
return d;
}
if(n < 0) {
return helperPow(x,++n, d/x);
}
return helperPow(x, --n, d*x);
}

Using your current approach, the number of levels of recursion will be equal to n, so it will definitely cause a StackOverflow exception as it requires considerable stack space.
Note that if n is an even number, x^n = (x ^ (n/2)) * (x ^ (n/2)).
If n is an odd number, x^n = (x ^ (n/2)) * (x ^ (n/2)) * x.
So you can reduce the number of levels of recursion to log(n), which will definitely not cause a Stackoverflow exception even if n is large (In your case, n is 2147483647, it will take 31 recursions):
public double myPow(double x, int n) {
return n >= 0 ? helperPow(x, n) : 1.0 / helperPow(x, -n);
}
public double helperPow(double x, long n) {
if (n == 0) {
return 1.0;
}
double y = helperPow(x, n / 2);
return n % 2 == 0 ? y * y : y * y * x;
}

Related

Java method like MathPow with a solution iterative and recursive in efficiency-Homework

I have a problem with my homework and i need help please!
Question 1:
Complete the Java methods below so that raiseToPower(x,n) will raise the number x to an integer power n (that is, to calculate the value xn ). Remember that x-n = 1/xn,
and that x0 = 1.
You should do this in the fewest number of steps possible (that is, in O(log n) time).
Give a solution which is non-recursive (iterative):
This is my solution :
public static double raiseToPower (double x, int n) {
double res=1;
boolean neg=false;
if(n<0)
{
neg=true;
}
if(n>0)
for (int i=0;i<n;i++) {
res = res * x;
}
if(neg==true) {
n=n*-1;
for (int i=0;i<n;i++) {
res = res * x;
}
res=1/res;
}
return res;
}
but this is not correct because is not efficiency
This my error for example:
52.49 to the power of 9 solved in 9 steps, but it could have been done in 4 steps
89.89 to the power of 75 solved in 75 steps, but it could have been done in 7 steps
78.57 to the power of 63 solved in 63 steps, but it could have been done in 6 steps
70.17 to the power of 44 solved in 44 steps, but it could have been done in 6 steps
Note:must not be used in method java.lang.MathPow
Question 2:
I need write code exactly like Question 1 but in recursive
This is my Question:
Give a recursive solution:
This is my code:
public static double raiseToPower (double x, int n) {
ouble dev=0.0;
if (n == 0) {
return 1;
} else {
if (n < 0) {
double count= raiseToPower (x, n+1);
dev=count*x;
return 1 / raiseToPower (x, -n);
}
if (n > 0) {
double count= raiseToPower (x, n-1);
dev=count*x;
}
}
return dev;
}
This code is correct but not efficiency.
This is my error for example:
53.31 to the power of 44 solved in 44 steps, but it could have been done in 6 steps
6.90 to the power of 74 solved in 74 steps, but it could have been done in 7 steps
80.76 to the power of 76 solved in 76 steps, but it could have been done in 7 steps
51.44 to the power of 86 solved in 86 steps, but it could have been done in 7 steps
76.26 to the power of 50 solved in 50 steps, but it could have been done in 6 steps
63.53 to the power of 93 solved in 93 steps, but it could have been done in 7 steps
Note:must not be used in method java.lang.MathPow
Thank you everyone for helping and solving both problems !!!
You can calculate in O(logN) x^n by breaking down n in 2's powers, like so:
9 = 1+8
15= 1+2+4+8
Therefore, x^9= (x^1)*(x^8).
In order to break down n in 2's powers, you can use bitwise operators. Like this: n&pow2 would mean you do the "AND" operation between N and pow2, which means if n has a bit 1 and pow2 also has that bit 1, the result will be non-zero. Given that pow2 must have a single bit 1( it is a power of 2), you can basically check every bit of n. So you are breaking down n in the powers of 2 and you can simply keep a powx around that means x^(pow2) as you are looping through the powers of 2, then multiply it to res whenever you find that n indeed is composed of that power of 2.
So we can make this code for the first solution:
public static double raiseToPower (double x, int n) {
double res=1;
double powx=x;
int pow2=1;
boolean neg=false;
if(n<0)
{
neg=true;
n=n*-1;
}
while(n!=0) {
if((n&pow2)!=0)
{
res=res*powx;
n=n-pow2;
}
powx=powx*powx;
pow2=pow2*2;
}
if(neg==true)
res=1/res;
return res;
}
Here's more articles about bitwise operators: https://www.tutorialspoint.com/java/java_basic_operators.htm
Similarly, you can modify the recursive code to get it in O(logN).
Here would be the recursive code:
public static double raiseToPower(double x, int n)
{
boolean neg= false;
double res=1;
if(n<0)
{
neg=true;
n=-n;
}
if (n == 0) return 1;
if (n % 2 == 0)
{
res= raiseToPower(x, n / 2);
res=res*res;
}
else
{
res= x * raiseToPower(x, n - 1);
}
if(!neg)
return res;
return 1/res;
}
public class ExponentialCalculator {
public static void main(String[] args) {
double x = 2;
int n = -4;
System.out.println(raiseToPower(x, n));
}
//Divide and Conquer method
public static double raiseToPower (double x, int n) {
if(n==0) {
return 1;
}
double temp = raiseToPower(x, n/2) * raiseToPower(x, n/2);
if(n%2==0) {
return n > 0 ? temp: 1/temp;
}
else {
return n > 0 ? x * temp: 1/(x * temp);
}
}
}
result 0.0625
Complexity Log(n)

Recursive method for x^n optimised for when n is even

I need to write a recursive method using Java called power that takes a double x and an integer n and that returns x^n. Here is what I have so far.
public static double power(double x, int n) {
if (n == 0)
return 1;
if (n == 1)
return x;
else
return x * (power(x, n-1));
}
This code works as expected. However, I am trying to go the extra mile and perform the following optional exercise:
"Optional challenge: you can make this method more efficient, when n is even, using x^n = (x^(n/2))^2."
I am not sure how to implement that last formula when n is even. I do not think I can use recursion for that. I have tried to implement the following, but it also does not work because I cannot take a double to the power of an int.
if (n%2 == 0)
return (x^(n/2))^2;
Can somebody point me in the right direction? I feel like I am missing something obvious. All help appreciated.
It's exactly the same principle as for x^n == x*(x^(n-1)): Insert your recursive function for x^(n/2) and (...)^2, but make sure you don't enter an infinite recursion for n == 2 (as 2 is even, too):
if (n % 2 == 0 && n > 2)
return power(power(x, n / 2), 2);
}
Alternatively, you could just use an intermediate variable:
if (n % 2 == 0) {
double s = power(x, n / 2);
return s * s;
}
I'd probably just handle 2 as a special case, too -- and avoid the "and"-condition and extra variable:
public static double power(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
if (n == 2) return x * x;
if (n % 2 == 0) return power(power(x, n / 2), 2);
return x * (power(x, n - 1));
}
P.S. I think this should work, too :)
public static double power(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
if (n == 2) return x * x;
return power(x, n % 2) * power(power(x, n / 2), 2);
}
When n is even, the formula is exactly what you wrote: divide n by two, call power recursively, and square the result.
When n is odd, the formula is a little more complex: subtract 1 from n, make a recursive call for n/2, square the result, and multiply by x.
if (n%2 == 0)
return (x^(n/2))^2;
else
return x*(x^(n/2))^2;
n/2 truncates the result, so subtraction of 1 is not done explicitly. Here is an implementation in Java:
public static double power(double x, int n) {
if (n == 0) return 1;
if (n == 1) return x;
double pHalf = power(x, n/2);
if (n%2 == 0) {
return pHalf*pHalf;
} else {
return x*pHalf*pHalf;
}
}
Demo.
Hint: The ^ operation won't perform exponentiation in Java, but the function you wrote, power will.
Also, don't forget squaring a number is the same as just multiplying it by itself. No function call needed.
Making a small change to your function, it will reduce the number of recursive calls made:
public static double power(double x, int n) {
if (n == 0) {
return 1;
}
if (n == 1) {
return x;
}
if (n % 2 == 0) {
double temp = power(x, n / 2);
return temp * temp;
} else {
return x * (power(x, n - 1));
}
}
Since
x^(2n) = (x^n)^2
you can add this rule to your method, either using the power function you wrote, as Stefan Haustein suggested, or using the regular multiplication operator, since it seems you are allowed to do that.
Note that there is no need for both the base cases n=1 and n=0, one of them suffices (prefferably use the base case n=0, since otherwise your method would not be defined for n=0).
public static double power(double x, int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
double val = power(x, n/2);
return val * val;
else
return x * (power(x, n-1));
}
There is no need to check that n>2 in any of the cases.
This just reminds me more optimisation could be done
and this following code.
class Solution:
# #param x, a float
# #param n, a integer
# #return a float
def pow(self, x, n):
if n<0:
return 1.0/self.pow(x,-n)
elif n==0:
return 1.0
elif n==1:
return x
else:
m = n & (-n)
if( m==n ):
r1 = self.pow(x,n>>1)
return r1*r1
else:
return self.pow(x,m)*self.pow(x,n-m)
what is more intermediate result could be memorised and avoid redundant computation.

how to implement pow(double x, double y)?

I know how to impelment pow(double x, int y)
public class Solution {
public double myPow(double x, int n) {
if (n == 0)
return 1;
if (n % 2 == 0) {
return myPow(x * x, n / 2);
} else {
if (n > 0)
return x * myPow(x, n - 1);
else
return 1 / x * myPow(x, n + 1);
}
}
}
But how to make it handle double y?
Exponentiation with non-integer values is done mathematically with infinite series:
See here under "Exponential Series Expansion" and "Exponential Theorem"
Basically, you'll use a known infinite series of terms, and calculate a number of them that satisfies your numerical precision requirements.
If i recall correctly you need to write a series expansion for log (natural logarithm) and exp (exponent).Sincelog(x^y) = y*log(x) andexp(log(x)) = xyou can evaluate the result. I think I used the Taylor series for this.wikipedia

Recursive "Power Of" function

We are given an assignment to solve a recursive function that can calculate the power of a number using the following rules (took a snapshot):
http://i.imgur.com/sRoQJ1j.png
I cant seem to figure out to do this as I have been trying for the past 4 hours.
My attempt:
public static double power(double x, int n) {
if(n == 0) {
return 1;
}
// x^n = ( x^n/2 )^ 2 if n > 0 and n is even
if(n % 2 == 0) {
double value = ((x * power(x, n/2)) * x);
return value;
} else {
double value = x * ((x * power(x, n/2)) * x);
return value;
}
}
I believe I am doing wrong when I am multiplying x with the recursive function whereas I should be multiplying by x (power Of = x * x * .... x(n))...
I also can see that in this statement:
double value = ((x * power(x, n/2)) * x);
It is wrong because I am not squaring the value but just multiplying it with x. I think I need to store it in a variable first, then do something like value * value to square the end result - but that just gives me a huge number.
Any help is appreciated, thanks.
The problem is in this statement:
if(n % 2 == 0) {
double value = ((x * power(x, n/2)) * x);
return value;
It is both syntactically (the brackets don't match) as semantically (deeper recursion, incorrect).
It should be replaced with:
if(n % 2 == 0) {
double value = power(x*x, n/2);
return value;
The reason is that:
x^(2*k) = (x^2)^k
Which is almost literally what is implemented in the code. Because we know n is even, there is a k = n/2, such that the above equation holds.
The same with the odd case:
else {
double value = x * power(x*x, n/2);
return value;
This because:
x^(2*k+1) = x*x^(2*k) (version of #rgettman)
With k = (n-1)/2, which can be further optimized to:
x^(2*k+1) = x*x^(2*k)=x*(x^2)^k (our version)
You can further optimize this as:
public static double power(double x, int n) {
if(n == 0) {
return 1;
}
double xb = x*x;
if((n&0x01) == 0x00) {
return power(xb,n>>>0x01);
} else {
return x*power(xb,n>>>0x01);
}
}
Or, you could transform the recursive aspect into a while algorithm (since it is mainly tail-recursion:
public static double power(double x, int n) {
double s = 1.0d;
while(n != 0) {
if((n&0x01) != 0x00) {
s *= x;
}
n >>>= 0x01;
x *= x;
}
return s;
}
Performance:
three different versions were implemented. This jdoodle shows benchmark tests. With:
power1 the version of #rgettman;
power2 the proposed recursive version in this answer; and
power3 the non-recursion version.
In case one sets L to 10'000'000 (thus calculating all power from 0 up to L), one run results in:
L = 10M L=20M
power1: 1s 630 018 699 3s 276 492 791
power2: 1s 396 461 817 2s 944 427 704
power3: 0s 803 645 986 2s 453 738 241
Don't take the numbers after the comma very seriously. Although Java measures in nano-seconds, these numbers are extremely inaccurate and have more to do with side-events (like OS calls, cache-faults,...) than with the real program runtime.
In your "even" case, you can calculate it with the recursive call, passing n/2 as you've done. But you need to multiply the result with itself to square it.
double value = power(x, n/2);
value *= value;
return value;
In your "odd" case, you can multiply x by the recursive call of the exponent minus 1.
double value = x * (power(x, n - 1));
return value;
You need to understand how recursion works internally. Logic is simple is to store the function calls and returned values in a stack. When the method termination condition is reached, pop out the values and return from method.
You need simply this:
public static double power(double x, int n) {
if(n == 0) {
return 1;
} else {
double value = (x * power(x, (n-1)));
return value;
}
}

recursion dilemma

I'm trying to create code to find the GCD of three user inputed numbers. My goal is to call the method with the inputed numbers, divide by q which is initialized as 1, record q only if the remainder is zero, then determine if its greater or equal to the smallest number, if it is not, increase q and recall method but if it is i would like to print out the largest last recorded q, what am i doing wrong? i keep getting stack overflow errors.
public static int recursiveMethod (int x, int y, int z, int q)
{
int largestVar;
int xRes = x % q;
int yRes = y % q;
int zRes = z % q;
if (xRes==0 && yRes ==0 && zRes==0) {
largestVar = q;
if (q >= x && q >= y && q >= z)
{
return largestVar;
}
}
else {
q++;
}
return recursiveMethod(x, y, z, q);
One of the mistakes I noticed, your if condition is wrong:
if (q >= x && q >= y && q >= z)
GCD of these 3 numbers is less than or equal to each of them, so change it to:
if (x >= q && y >= q && z >= q)
If you are testing numbers iteratively like that, you should start from a certain number and countdown so that you can guarantee that a number meeting the condition is the actual GCD. And that certain starting number must be the minimum of 3 numbers.
And fully working version of your method is here:
public static int recursiveMethod(int x, int y, int z, int q)
{
int largestVar;
int xRes = x % q;
int yRes = y % q;
int zRes = z % q;
if (xRes == 0 && yRes == 0 && zRes == 0) {
largestVar = q;
if (x >= q && y >= q && z >= q) {
return largestVar;
}
} else {
q--;
}
return recursiveMethod(x, y, z, q);
}
Sample call:
int x = recursiveMethod(30, 60, 45, 30); // x = 15 after this execution.
Don't forget that gcd(x, y, z) = gcd(gcd(x, y), z). So, if you want a simpler algorithm, you can implement gcd with two numbers and then call that method for 3 numbers.
public static int GCD(int x, int y) {
if (y == 0) return x;
return GCD(x, x % y);
}
And then for three numbers:
public static int GCDfor3 (int x, int y, int z) {
return GCD(GCD(x, y), z)
}
You first case will be 1, at which point, xRes==0 && yRes ==0 && zRes==0 is surely true, and you set largestVar to 1. Then, since 1 is probably less than the variables passed in, it continues, does not increment q in the else block, and calls recursiveMethod with q=1 again! And the process repeats itself.
public static int recursiveMethod (int x, int y, int z, int q, int largestVar)
{
int xRes = x % q;
int yRes = y % q;
int zRes = z % q;
if (xRes==0 && yRes ==0 && zRes==0) {
//A common denominator found!
largestVar = q;
}
//regardless whether a denominator was found, check for the termination condition
//Or works here, by the way, since we only need to know when q is greater than one of them.
if (q >= x || q >= y || q >= z) {
return largestVar;
}
//if we don't terminate, increment q and recurse.
q++;
return recursiveMethod(x, y, z, q, largestVar);
}
Modified to correctly handle largestVar. Wasn't paying attention to that bit. Being locally scoped, the value of largestVar will not be maintained through recursive calls, so it must also be passed into the recursiveMethod call (either that, or be declared as a class scope variable, or some such). An appropriate starting value for it would be 0.
If you are incrementing Q by one each time , with suitable numbers you will always run out of stack. I would use the euclidean algorithm for each pair of numbers and then start from there. I mean if it is common between a,b,c it must be common factor between a and b or a and c or c and b, right?
https://math.stackexchange.com/questions/85830/how-to-use-the-extended-euclidean-algorithm-manually

Categories

Resources