Calculating Square Root

Page 1 of 1

11 Replies - 9818 Views - Last Post: 10 August 2010 - 03:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=185347&amp;s=94a0f486427b964a85e9b6197a17ed98&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 4
• Posts: 45
• Joined: 12-July 10

Calculating Square Root

Posted 10 August 2010 - 03:10 AM

Calculate the square root of a number without using any library function. Here's my code:

```
import java.io.*;
class squareRoot
{
public static void main(String args[])throws IOException
{
System.out.println("Enter number");
int sq = 0;
for(int i=1; i<(n/2); i++)
{
if((i*i) == n)
{
sq = i;
break;
}
}
if(sq == 0) System.out.println("Not a perfect square");
else System.out.println("Square root = " + sq);
}
}

```

But this would calculate the square root of a perfect square like 25 or 36 only. So how should I write some code which can calculate the square root of any number?

Maybe I could use double type variables for i (loop variable) and sq, and increase i by something like 0.001. But this would make the program highly ineffiecient and time-consuming and still incompetent for some numbers.

This post has been edited by adhish94: 10 August 2010 - 03:17 AM

Is This A Good Question/Topic? 0

Replies To: Calculating Square Root

#2 cfoley

• Cabbage

Reputation: 2195
• Posts: 4,615
• Joined: 11-December 07

Re: Calculating Square Root

Posted 10 August 2010 - 04:00 AM

Using a double is a good move. Look up Newton's method. In a nutshell, knows the square root is between 0 and the number so it makes an initial guess at the middle. Now it knows it's between the middle and one of the previous bounds. Keep on going until you have the precision you want.

#3 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 05:42 AM

Here's a quick and easy little equation you could follow:

-Get the number(n) you want to find the sqrt of
-Plug it in to this equation x = .5 * ((x+n) / x) where x starts at 1
-Do that equation for however big the number you're finding the sqrt of is

And yes, remember to use doubles for sure!

Hope this helps!

This post has been edited by eZACKe: 10 August 2010 - 05:43 AM

#4 NeoTifa

• Whorediot

Reputation: 2999
• Posts: 16,362
• Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:06 AM

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too

This post has been edited by NeoTifa: 10 August 2010 - 06:11 AM

#5 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 06:16 AM

NeoTifa, on 10 August 2010 - 09:06 AM, said:

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too

Oh? I thought he was just referring to the Methods defined in the Math library.

#6 NeoTifa

• Whorediot

Reputation: 2999
• Posts: 16,362
• Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:20 AM

Wait, did you mean * to be a variable? XD

#7 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 06:25 AM

No haha I just meant it to be multiplication. I thought he just wanted an alternate way of doing it without using sqrt(). I was under the assumption that being unable to use that method was his only limitation.

#8 NeoTifa

• Whorediot

Reputation: 2999
• Posts: 16,362
• Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 06:58 AM

Whoops, I missed the whole parenthesis jumble. XD Sorry.

#9 eZACKe

• Garbage Collector

Reputation: 120
• Posts: 1,278
• Joined: 01-June 09

Re: Calculating Square Root

Posted 10 August 2010 - 07:07 AM

Not a problem.

#10 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:16 PM

NeoTifa, on 10 August 2010 - 07:06 AM, said:

But x = .5* would mean he'd have to use the math library for the *.

I found this site, might help. This too

Woldn't consider * as being in the Math library actually it is not ?
Figuring out a sqrt without the * operator would be a real challenge using iterations but aren't + and - in the Math lib too ?

#11 NeoTifa

• Whorediot

Reputation: 2999
• Posts: 16,362
• Joined: 24-September 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:18 PM

Lol I thought he was using it as some type of variable. Was skimming. Lol trying to do this without java.lang would be epic

#12 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

Re: Calculating Square Root

Posted 10 August 2010 - 03:21 PM

NeoTifa, on 10 August 2010 - 04:18 PM, said:

Lol I thought he was using it as some type of variable. Was skimming. Lol trying to do this without java.lang would be epic

If you want to try, I'll be glad to help

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }