# Calculating Square Root

Page 1 of 1

## 11 Replies - 11609 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=7051f3b4b1ee08bedcf7b564e16c855c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 adhish94

• New D.I.C Head

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
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int n = Integer.parseInt(br.readLine());
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: 2388
• Posts: 5,013
• 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.
Was This Post Helpful? 1

### #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

Was This Post Helpful? 1

### #4 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,393
• 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

Was This Post Helpful? 1

### #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.
Was This Post Helpful? 0

### #6 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,393
• Joined: 24-September 08

## Re: Calculating Square Root

Posted 10 August 2010 - 06:20 AM

Wait, did you mean * to be a variable? XD
Was This Post Helpful? 0

### #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.
Was This Post Helpful? 0

### #8 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,393
• Joined: 24-September 08

## Re: Calculating Square Root

Posted 10 August 2010 - 06:58 AM

Whoops, I missed the whole parenthesis jumble. XD Sorry.
Was This Post Helpful? 0

### #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.
Was This Post Helpful? 0

### #10 pbl

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

Reputation: 8378
• Posts: 31,956
• 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 ?
Was This Post Helpful? 0

### #11 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,393
• 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
Was This Post Helpful? 0

### #12 pbl

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

Reputation: 8378
• Posts: 31,956
• 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
Was This Post Helpful? 0

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; }