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;
}
}
Related
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;
}
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)
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.
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
I have to write a power method in Java. It receives two ints and it doesn't matter if they are positive or negative numbers. It should have complexity of O(logN). It also must use recursion. My current code gets two numbers but the result I keep outputting is zero, and I can't figure out why.
import java.util.Scanner;
public class Powers {
public static void main(String[] args) {
float a;
float n;
float res;
Scanner in = new Scanner(System.in);
System.out.print("Enter int a ");
a = in.nextFloat();
System.out.print("Enter int n ");
n = in.nextFloat();
res = powers.pow(a, n);
System.out.print(res);
}
public static float pow(float a, float n) {
float result = 0;
if (n == 0) {
return 1;
} else if (n < 0) {
result = result * pow(a, n + 1);
} else if (n > 0) {
result = result * pow(a, n - 1);
}
return result;
}
}
Let's start with some math facts:
For a positive n, aⁿ = a⨯a⨯…⨯a n times
For a negative n, aⁿ = ⅟a⁻ⁿ = ⅟(a⨯a⨯…⨯a). This means a cannot be zero.
For n = 0, aⁿ = 1, even if a is zero or negative.
So let's start from the positive n case, and work from there.
Since we want our solution to be recursive, we have to find a way to define aⁿ based on a smaller n, and work from there. The usual way people think of recursion is to try to find a solution for n-1, and work from there.
And indeed, since it's mathematically true that aⁿ = a⨯(aⁿ⁻¹), the naive approach would be very similar to what you created:
public static int pow( int a, int n) {
if ( n == 0 ) {
return 1;
}
return ( a * pow(a,n-1));
}
However, the complexity of this is O(n). Why? Because For n=0 it doesn't do any multiplications. For n=1, it does one multiplication. For n=2, it calls pow(a,1) which we know is one multiplication, and multiplies it once, so we have two multiplications. There is one multiplication in every recursion step, and there are n steps. So It's O(n).
In order to make this O(log n), we need every step to be applied to a fraction of n rather than just n-1. Here again, there is a math fact that can help us: an₁+n₂ = an₁⨯an₂.
This means that we can calculate aⁿ as an/2⨯an/2.
But what happens if n is odd? something like a⁹ will be a4.5⨯a4.5. But we are talking about integer powers here. Handling fractions is a whole different thing. Luckily, we can just formulate that as a⨯a⁴⨯a⁴.
So, for an even number use an/2⨯an/2, and for an odd number, use a⨯ an/2⨯an/2 (integer division, giving us 9/2 = 4).
public static int pow( int a, int n) {
if ( n == 0 ) {
return 1;
}
if ( n % 2 == 1 ) {
// Odd n
return a * pow( a, n/2 ) * pow(a, n/2 );
} else {
// Even n
return pow( a, n/2 ) * pow( a, n/2 );
}
}
This actually gives us the right results (for a positive n, that is). But in fact, the complexity here is, again, O(n) rather than O(log n). Why? Because we're calculating the powers twice. Meaning that we actually call it 4 times at the next level, 8 times at the next level, and so on. The number of recursion steps is exponential, so this cancels out with the supposed saving that we did by dividing n by two.
But in fact, only a small correction is needed:
public static int pow( int a, int n) {
if ( n == 0 ) {
return 1;
}
int powerOfHalfN = pow( a, n/2 );
if ( n % 2 == 1 ) {
// Odd n
return a * powerOfHalfN * powerOfHalfN;
} else {
// Even n
return powerOfHalfN * powerOfHalfN;
}
}
In this version, we are calling the recursion only once. So we get from, say, a power of 64, very quickly through 32, 16, 8, 4, 2, 1 and done. Only one or two multiplications at each step, and there are only six steps. This is O(log n).
The conclusion from all this is:
To get an O(log n), we need recursion that works on a fraction of n at each step rather than just n - 1 or n - anything.
But the fraction is only part of the story. We need to be careful not to call the recursion more than once, because using several recursive calls in one step creates exponential complexity that cancels out with using a fraction of n.
Finally, we are ready to take care of the negative numbers. We simply have to get the reciprocal ⅟a⁻ⁿ. There are two important things to notice:
Don't allow division by zero. That is, if you got a=0, you should not perform the calculation. In Java, we throw an exception in such a case. The most appropriate ready-made exception is IllegalArgumentException. It's a RuntimeException, so you don't need to add a throws clause to your method. It would be good if you either caught it or prevented such a situation from happening, in your main method when you read in the arguments.
You can't return an integer anymore (in fact, we should have used long, because we run into integer overflow for pretty low powers with int) - because the result may be fractional.
So we define the method so that it returns double. Which means we also have to fix the type of powerOfHalfN. And here is the result:
public static double pow(int a, int n) {
if (n == 0) {
return 1.0;
}
if (n < 0) {
// Negative power.
if (a == 0) {
throw new IllegalArgumentException(
"It's impossible to raise 0 to the power of a negative number");
}
return 1 / pow(a, -n);
} else {
// Positive power
double powerOfHalfN = pow(a, n / 2);
if (n % 2 == 1) {
// Odd n
return a * powerOfHalfN * powerOfHalfN;
} else {
// Even n
return powerOfHalfN * powerOfHalfN;
}
}
}
Note that the part that handles a negative n is only used in the top level of the recursion. Once we call pow() recursively, it's always with positive numbers and the sign doesn't change until it reaches 0.
That should be an adequate solution to your exercise. However, personally I don't like the if there at the end, so here is another version. Can you tell why this is doing the same?
public static double pow(int a, int n) {
if (n == 0) {
return 1.0;
}
if (n < 0) {
// Negative power.
if (a == 0) {
throw new IllegalArgumentException(
"It's impossible to raise 0 to the power of a negative number");
}
return 1 / pow(a, -n);
} else {
// Positive power
double powerOfHalfN = pow(a, n / 2);
double[] factor = { 1, a };
return factor[n % 2] * powerOfHalfN * powerOfHalfN;
}
}
pay attention to :
float result = 0;
and
result = result * pow( a, n+1);
That's why you got a zero result.
And instead it's suggested to work like this:
result = a * pow( a, n+1);
Beside the error of initializing result to 0, there are some other issues :
Your calculation for negative n is wrong. Remember that a^n == 1/(a^(-n)).
If n is not integer, the calculation is much more complicated and you don't support it. I won't be surprised if you are not required to support it.
In order to achieve O(log n) performance, you should use a divide and conquer strategy. i.e. a^n == a^(n/2)*a^(n/2).
Here is a much less confusing way of doing it, at least if your not worred about the extra multiplications. :
public static double pow(int base,int exponent) {
if (exponent == 0) {
return 1;
}
if (exponent < 0) {
return 1 / pow(base, -exponent);
}
else {
double results = base * pow(base, exponent - 1);
return results;
}
}
# a pow n = a pow n%2 * square(a) pow(n//2)
# a pow -n = (1/a) pow n
from math import inf
def powofn(a, n):
if n == 0:
return 1
elif n == 1:
return a
elif n < 0:
if a == 0 : return inf
return powofn(1/a, -n)
else:
return powofn(a, n%2) * powofn(a*a, n//2)
A good rule is to get away from the keyboard until the algorythm is ready. What you did is obviously O(n).
As Eran suggested, to get a O(log(n)) complexity, you have to divide n by 2 at each iteration.
End conditions :
n == 0 => 1
n == 1 => a
Special case :
n < 0 => 1. / pow(a, -n) // note the 1. to get a double ...
Normal case :
m = n /2
result = pow(a, n)
result = resul * resul // avoid to compute twice
if n is odd (n % 2 != 0) => resul *= a
This algorythm is in O(log(n)) - It's up to you to write correct java code from it
But as you were told : n must be integer (negative of positive ok, but integer)
import java.io.*;
import java.util.*;
public class CandidateCode {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc. nextInt();
int result = power(m,n);
System.out.println(result);
}
public static int power(int m, int n){
if(n!=0)
return (m*power(m,n-1));
else
return 1;
}
}
try this:
public int powerN(int base, int n) {return n == 0 ? 1 : (n == 1 ? base : base*(powerN(base,n-1)));
ohk i read solutions of others posted her but let me clear you those answers have given you
the correct & optimised solution but your solution can also works by replacing float result=0 to float result =1.