# Thread: Creating a New Method for Square Root Loop

1. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## 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: 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: The output is the result of this second formula. Now, these are the problems I'm having creating this:
Last edited by SapphireSpark; 10-25-2008 at 09:26 AM.  Reply With Quote

2. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Here's what I have so far, I'm sure there's mistakes in it already:

Java 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?
Last edited by SapphireSpark; 10-25-2008 at 09:31 AM.  Reply With Quote

3. Member Join Date
Oct 2008
Location
UK
Posts
65
Rep Power
0

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

Java 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:

Java 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:

Java Code:
`for (int i = 0; i<n; i++)`  Reply With Quote

4. ## 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 :)  Reply With Quote

5. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## 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.  Reply With Quote

6. ##  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.
Java 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;
}
}```
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.  Reply With Quote

7. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## 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?  Reply With Quote

8. Senior Member Join Date
Sep 2008
Posts
564
Rep Power
13

## 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.  Reply With Quote

9. ##  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  Reply With Quote

10. Member Join Date
Oct 2008
Posts
5
Rep Power
0

## SapphireSpark are you in Hong Zhangs class? I am in there too if you are. lol PM me or something.  Reply With Quote

11. Member Join Date
Oct 2008
Posts
38
Rep Power
0

## Yes, I am! Dude, I'm the indian girl in the back. Who are you?  Reply With Quote

12. Member Join Date
Dec 2008
Posts
2
Rep Power
0

## how to make asterisks triangle using java code

how to make an asterisk triangle using java could i hope you can help me..?  Reply With Quote

13. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
13

## 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  Reply With Quote

14. ## thread is 4 months' old, is that a zoombie?  Reply With Quote

15. ## Zombie alert...  Reply With Quote