# Creating a New Method for Square Root Loop

• 10-25-2008, 09:18 AM
SapphireSpark
Creating a New Method for Square Root Loop
Hi! I'm writing a program that uses a while new method to calculate the square root of a. We can't use the Math class at all, we have to make this method using Newton's method and the user-inputed integer n:

http://fc81.deviantart.com/fs34/f/20...pfruitloop.jpg

Then I have to set the initial value of x^0 = 1, and perform 1000 iterations. (I'm not totally sure why or how to do that. I don't think x^n should be increasing like x^n++ or anything.) Then I have to call this method from the main method to compute the following expression, where there are n radicals:

http://fc59.deviantart.com/fs33/f/20...pfruitloop.jpg

The output is the result of this second formula. Now, these are the problems I'm having creating this:
• 10-25-2008, 09:22 AM
SapphireSpark
Here's what I have so far, I'm sure there's mistakes in it already:

Code:

```import java.util.Scanner; public class SquareRoot {     public static void main(String[] args) {   Scanner scan = new Scanner (System.in);         System.out.println("Enter n: ");         int n = scan.nextInt();         //double a = (eventually the main method will plug values into a)         double x = 1;         for (int i = 0; i<1000; i++) {             x = 0.5*(x+a/x);         }         System.out.println("sqrt of " + a + "is " + x);     } }```
-First of all, I'm not sure how this can perform 1000 iterations if it doesn't know what a is yet.

-How do I make the method apply within itself n times? I'm thinking it's a for loop wth a counter increases until it reaches n, but I don't know how to put the method in it, or what else to do it. Also, should the Newton's method part be in its own class?
• 10-25-2008, 09:44 AM
Paul Richards
Quote:

Originally Posted by SapphireSpark
Here's what I have so far, I'm sure there's mistakes in it already:

Code:

```import java.util.Scanner; public class SquareRoot {     public static void main(String[] args) {   Scanner scan = new Scanner (System.in);         System.out.println("Enter n: ");         int n = scan.nextInt();         //double a = (eventually the main method will plug values into a)         double x = 1;         for (int i = 0; i<1000; i++) {             x = 0.5*(x+a/x);         }         System.out.println("sqrt of " + a + "is " + x);     } }```
-First of all, I'm not sure how this can perform 1000 iterations if it doesn't know what a is yet.

-How do I make the method apply within itself n times? I'm thinking it's a for loop wth a counter increases until it reaches n, but I don't know how to put the method in it, or what else to do it. Also, should the Newton's method part be in its own class?

This will work fine if you initialise a, e.g. like this:

Code:

`double a = (double) n;`
If you don't declare and initialise a, the code won't compile, so it won't perform any iterations!

I think this is fine inside one class, you don't need any others.

To apply it n times, declare and initialise n, and use this:

Code:

`for (int i = 0; i<n; i++)`
• 10-25-2008, 09:47 AM
andy_man
I think "a" should be inserted as well as you've inserted value for "n" variable.

And for the other question, I think you can use recursion which will help you to solve the problem with iteration. If you don't know what is recursion or how to "use" it, search it in google :)
• 10-25-2008, 09:47 AM
SapphireSpark
ok. But I thought a didn't equal n, it was the subject of the radical, not the number of radicals, n.

Thanks, Andy, but I think the teacher'd get suspicious if I used something like recursion, which we haven't covered in class yet.
• 10-27-2008, 06:31 PM
xcallmejudasx
Quote:

Originally Posted by SapphireSpark
ok. But I thought a didn't equal n, it was the subject of the radical, not the number of radicals, n.

Thanks, Andy, but I think the teacher'd get suspicious if I used something like recursion, which we haven't covered in class yet.

Not necessarily. If you have been taught how to call methods within a class you can accidentally stumble upon it. Recursion is just where a method calls itself which would work perfect for your example. Lets say you have a method called calculateRoot(some number). You do the equation to calculate the root and then you need the root of that answer so you send it back to the calculateRoot method.
Code:

```public void calculateRoot(int number){           while (number >= 1){                   //do your equation to calculate the root                   System.out.println("The root of "+number+" is "+answerfromcalculation;                   calculateRoot(answerfromcalculation);                 } }```
Thats pretty much it. It's fairly common for students to come across recursion on their own. Specially when using a specific operation over and over.

I strayed off my original intention for this post. I was going to say my friend, whom has no previous programming experience, ended up using recursion on accident for a similar problem. The teacher was a little surprised and explained to us what recursion was after his little mistake.
• 10-29-2008, 06:17 AM
SapphireSpark
Hmm, that sounds pretty useful. I'm still just practicing calling methods.

So, " calculateRoot(answerfromcalculation);" is what makes it send the answer back to the method?
• 10-29-2008, 08:07 AM
emceenugget
For your second problem, you don't need recursion. A loop will suit just fine and is much more efficient, as you have a set number of iterations in 'n'. It's one of those problems where if you try to solve it by hand that you can devise a loop to solve it, and isn't much different than the first algorithm you created. Just think about what you're taking the square root of.
• 10-29-2008, 04:04 PM
xcallmejudasx
Quote:

Originally Posted by SapphireSpark
Hmm, that sounds pretty useful. I'm still just practicing calling methods.

So, " calculateRoot(answerfromcalculation);" is what makes it send the answer back to the method?

yup that's exactly what sends it back. Just make sure you have a way to check (at the beginning of calculateRoot() ) that the number being brought in is greater then 1 or else it will become an endless root and keep square rooting 1, return 1, root 1, return 1, etc
• 10-30-2008, 04:27 AM
edgewater21
SapphireSpark are you in Hong Zhangs class? I am in there too if you are. lol PM me or something.
• 11-05-2008, 09:12 AM
SapphireSpark
Yes, I am! Dude, I'm the indian girl in the back. Who are you?
• 02-25-2009, 08:54 AM
nimboots
how to make asterisks triangle using java code
how to make an asterisk triangle using java could i hope you can help me..?
• 02-25-2009, 09:46 AM
mtyoung
first equation given by SapphireSpark is the general equation
of finding square root

as you said, x(0) = 1,
a in the equation should be the input integer
x(n) will be the square root of a
• 02-25-2009, 10:05 AM
angryboy