Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6

05202014, 12:06 AM #1
 Join Date
 May 2014
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Can someone help me with a simple while loop?
Exercise 7.2. Let’s say you are given a number, a, and you want to find its square root. One way to do that is to start with a very rough guess about the answer, x0, and then improve the guess using the following formula:
x1 = (x0 + a/x0)/2
For example, if we want to find the square root of 9, and we start with x0 = 6,
then x1 = (6 + 9/6)/2 = 15/4 = 3.75, which is closer.
We can repeat the procedure, using x1 to calculate x2, and so on. In this case, x2 = 3.075 and x3 = 3.00091. So that is converging very quickly on the right answer(which is 3).
Write a method called squareRoot that takes a double as a parameter and that returns an approximation of the square root of the parameter, using this technique. You may not use Math.sqrt.
As your initial guess, you should use a/2. Your method should iterate until it gets two consecutive estimates that differ by less than 0.0001; in other words, until the absolute value of xn − xn−1 is less than 0.0001. You can use Math.abs to calculate the absolute value.
So far, I have created a method that finds an approximation once.
public static double rootApprox(double a) {
double x0=a/2;
double x1=(x0+a/x0)/2;
System.out.println(x1);
x0=x1;
return x0;
}
but every time I repeat it, it doesn't yield the answer that it should yield. Also I have no idea how to incorporate the limits of the while loop... my solution was gonna be while(Math.abs(rootApprox(a)rootApprox(rootApprox(a)))>0.0001) but that didn't work. Thanks in advance
05202014, 06:56 AM
#2
You are over complicating things...
here is an algebraic tip... You can find any root by raising it to a power of a fraction. The denominator is the root and the numerator is the power the resultant root is raised by afterwards... some examples...
9^1/2 = 3
27^2/3 => 3^2 => 9
so something like this (pseudo code that will almost work as is)
I realize that is insanely giving the answer, but you need to go and look up how to code it correctly, AND research the theory behind it as you prof will probably ask where you got this idea from. Good luck, and thank you for posting the work you've tried on your own Homework questions are always welcome provided you put forth effort so imo you did well :PCode:private double findRoot(int number, int numerator, int denominator) { return number^(numerator/denominator); }
05212014, 02:38 AM
#3
 Join Date
 May 2014
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Yes, I can think of at least 3 different better ways to do it. But the book only taught me while loops thus far, and the author wants me to use a while loop to accomplish this goal. It is pointless, but I assume the book is just trying to challenge me. Obviously the ^1/2 works; but it's not what the problem asked me.
05212014, 02:39 AM
#4
 Join Date
 May 2014
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
couldn't edit, but my new code is:
public static double squareRoot(int a){
double x0=a/2;
while (a!=x0*x0){
x0=(x0+a/x0)/2;
}
return x0;
}
public static void main(String[] args){
System.out.println(squareRoot(9));
}
the only problem is that this only works for perfect squares. My condition in the while loop does not work for irrational numbers. Any ideas for one that will? Of course, following the context of the problem.
05222014, 09:12 PM
#5
Sorry I didn't see those stipulations in the original question wow that is a pain in the butt considering there are so many easier ways to do it. Anyways I think the problem might be because you are passing an int, so if you try to pass a double when it expects an int it will (I think) either throw an implicit conversion error or simply truncate the irrational part of the number (ie lose the double). See if it works changing it to squareRoot(double a) {...}
05222014, 10:18 PM
#6
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,994
 Thanks
 4
 Thanked 2,662 Times in 2,631 Posts
That would make sense to me as well.
You declare x0 as a double, but it will always be a truncated integer since you are dividing an int by an int. Either use a double for input or use 2.0 for your division amount.
In the above, both 8 and 9 will result in 4.0. This is because you are doing integer division and THEN casting it to a double. If you accept a double or divide by 2.0, than you will receive the correct double results.PHP Code:
public class ImplicitCast
{
public static double implicitCast(int in)
{
double out = in / 2;
return out;
}
public static void main(String... argv)
{
System.out.println(implicitCast(8));
System.out.println(implicitCast(9));
}
}
Also, you will see a lot of similar problems to this when you write and implement calculus equations. They are always based on the difference: take your current calculation and compare it to your last calculation. Is it different by more than x amount? If so, calculate again. When the difference is no longer greater than x amount, you consider the number to be "close enough" and return it as the result. x would be something like 0.0001 for example. So op, you'll want to keep this in mind when you look at your comparison clauses for while you continue looping.
Last edited by FouLu; 05222014 at 10:20 PM.
Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)PHP Code:
header('HTTP/1.1 420 Enhance Your Calm');