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

Results 1 to 7 of 7

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)
Code: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,995
 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.
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.
PHP Code:header('HTTP/1.1 420 Enhance Your Calm');

10232015, 11:33 PM #7
 Join Date
 Oct 2015
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
public class tester {
public static double squareRoot(double n){
double i = n/2;
while((i*i)n> 0.000000000000000000000000001){ /*the answer will become more and more accurate as you add more 0s. i*i is the approximation of n. Since the equation
will get you as close to the square root as possible but never get lower than that, i*i will be bigger than the value of n and will never contain the actual value of n. Hence, i*i n */
i = (i +n/i)/2;
}
return i;
}
public static void main(String[] args) {
System.out.println(squareRoot(9));
}
}
// Phew. It took me forever to figure this one out. Whoever made the method in Java library probably figured it out a million years ago, when they invented fire. I'm not smart like that. I'm pretty sure there will be variations of this while statement/ method.