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
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 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
The standard Ackermann formula as written in Java:
public static int ack(int x, int y) {
if (x == 0) {
return y + 1;
} else if (y == 0) {
return ack(x-1, 1);
} else {
// perforce (x > 0) && (y > 0)
return ack(x-1, ack(x,y-1));
}
}
I've been wondering - is there a faster version to implement this? I'm thinking maybe there is by using an accumulator or a loop.
Yes, for example by "cheating". If m is 5 or higher, none of the results can be represented by an int. For m = 4, only the n < 2 cases can be represented. For m < 4, there are simple closed formula's based on n.
Everything else would overflow anyway, so let's pretend those cases don't even happen (or you could throw an error or whatever).
Not tested:
int Ackerman(int m, int n) {
switch (m) {
case 0:
return n + 1;
case 1:
return n + 2;
case 2:
return n * 2 + 3;
case 3:
return (int)((1L << (n + 3)) - 3);
case 4:
return n == 0 ? 13 : 65533;
}
}
I can tell you one thing... int will not suffice for very many values of x and y
If you're going to be calling the function repetitively, you can create a int[][] array to store various values so you can look them up the second+ time around and only need to compute it once. But as for speeding up a single execution... not sure.
This variation is faster:
public static int ack(int x, int y) {
while (x != 0) {
y = y == 0 ? 1 : ack(x, y - 1);
x--;
}
return y + 1;
}
Is this possible guys? This is homework I have, and my teacher obviously believes it is, but it seems to me that it's impossible not to use addition or multiplication outside of the short-multiplication method.
Write (and provide a tester for) a recursive algorithm:
int multiply(int x, int y)
to multiply two positive integers together without using the *
operator. Do not just add x to itself y times!!!
(Hint: Write a recursive method that will multiply an integer by a
value in the range 0 .. 10. Then write a second recursive method to
implement the multiplication algorithm you learned to multiply
multi-digit numbers in elementary school.)
My issue is that once you break down any multi digit number and starting adding those together you have to use multiplication of numbers greater than 10, i.e 22 * 6 is 2 * 6 + 20 * 6 ... so am I totally missing something?
EDIT
I guess I should have added this is the code I have,
public int mult(int x, int y){
return x == 0 ? 0 : (mult(x-1, y) + y);
}
which is perfect, but as far as I understand the instructions, that's breaking do not just add x to itself y times. I personally believe it isn't, but my teacher hasn't been very clear, and I'd like to know if there's some other way that I havn't thought of, sorry for the confusion.
Yes, it's possible. Yes, I think you're missing something. Try writing down the steps you'd follow to manually multiply two numbers, the way you learned in elementary school.
Then turn those steps into code.
My interpretation of the assignment is that the teacher would like the student to implement a recursive algorithm to perform Grid method multiplication (the kind we learn in elementary school).
For example, multiplying 34 x 13 would be done like so...
34
* 13
====
12
90
40
+300
====
442
I didn't have easy access to a Java development environment, so I wrote the code in C# but the algorithm should be simple enough to convert into Java.
public int Multiply(int x, int y)
{
if (x < 0) throw new ArgumentException("must be positive integer", "x");
if (y < 0) throw new ArgumentException("must be positive integer", "y");
if (x == 0 || y == 0) return 0; // obvious quick-exit condition
// integer division
int xDivBy10 = x / 10;
int yDivBy10 = y / 10;
bool xIsSingleDigit = xDivBy10 == 0;
bool yIsSingleDigit = yDivBy10 == 0;
// base case
if (xIsSingleDigit && yIsSingleDigit)
{
return MultiplySingleDigits(x, y);
}
// otherwise, use grid multiplication recursively
// http://en.wikipedia.org/wiki/Grid_method_multiplication
if (xIsSingleDigit) // y must not be a single digit
{
return (Multiply(x, yDivBy10) * 10) + Multiply(x, y % 10);
}
if (yIsSingleDigit) // x must not be a single digit
{
return (Multiply(xDivBy10, y) * 10) + Multiply(x % 10, y);
}
// else - x and y are both numbers which are not single digits
return (Multiply(x, yDivBy10) * 10) + Multiply(x, y % 10); // the same code as the "if (xIsSingleDigit)" case
}
// technically, this algorith can multiply any positive integers
// but I have restricted it to only single digits as per the assignment's requirements/hint
private int MultiplySingleDigits(int x, int y)
{
if (x < 0 || x > 9) throw new ArgumentException("must be in range 0 - 9 (inclusive)", "x");
if (y < 0 || y > 9) throw new ArgumentException("must be in range 0 - 9 (inclusive)", "y");
if (x == 0 || y == 0) return 0; // base case
return x + MultiplySingleDigits(x, y - 1);
}
NOTES:
This approach still uses the * operator but not for actually multiplying x and y, it is used to increase other sub-products by multiples of 10
Many parts of this code could be simplified/refactored but I specifically left them expanded to make the steps more obvious
Of course you can do it.
First of all, think about the condition. If some number is 0, then the result is? Right.. zero.
So.. You'll have if x is zero or y is zero return 0
Now.. saying X * Y is like saying "X, Y times", which is like writing: X + .... + X (Y times).
So, you'll have something like:
x + multiply(x, y - 1);
You'll have to consider the case in which one of the numbers is negative (But if you understand the basic, I believe you can easily do it).
This solution will work for both when y>=0 and y<0
public int multiply(final int x, final int y) {
if (y != 0 && x != 0) {
if (y > 0) {
return multiply(x, y - 1) + x;
} else {
return multiply(x, y + 1) - x;
}
}
return 0;
}
Easily possible.
int multiply(int x, int y) {
if(y == 0) {
return 0;
}
return x + multiply(x, y - 1);
}
The above fails to take into account the case where y is negative, but you wouldn't want me to do all your work for you . . .
static int Multiply(int x, int y)
{
if (y > 0 && x > 0)
return (x + Multiply(x, y - 1));
if (y < 0 && x > 0)
return -Multiply(x, -y);
if (x < 0 && y > 0)
return -Multiply(-x, y);
if (x < 0 && y < 0)
return Multiply(-x, -y);
return 0;
}