Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Can someone help me with a simple while loop?

1. 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

• 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);
}```
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 :P

• 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.

• 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.

• 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) {...}

• 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));    }}  ```
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.

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.

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•