# Square Root without sqrt()

Page 1 of 1

## 9 Replies - 25166 Views - Last Post: 05 April 2011 - 04:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=226037&amp;s=0ddf8879177f25616b2d385264c8bf63&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Freakin' Oreo

Reputation: 1
• Posts: 2
• Joined: 02-April 11

# Square Root without sqrt()

Posted 04 April 2011 - 04:34 PM

Is there any way to calculate a square root without using the sqrt() function?
Thanks!

Oreo
Is This A Good Question/Topic? 1

## Replies To: Square Root without sqrt()

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• Posts: 45,424
• Joined: 27-December 08

## Re: Square Root without sqrt()

Posted 04 April 2011 - 04:35 PM

You can use the definition of: e^(ln(x)/2), which will calculate sqrt(x). e^ln(x) comes out to x, and x^(1/2) is the square root of x.

### #3 Freakin' Oreo

Reputation: 1
• Posts: 2
• Joined: 02-April 11

## Re: Square Root without sqrt()

Posted 04 April 2011 - 04:37 PM

macosxnerd101, on 04 April 2011 - 07:35 PM, said:

You can use the definition of: e^(ln(x)/2), which will calculate sqrt(x). e^ln(x) comes out to x, and x^(1/2) is the square root of x.

Thanks!
P.S You get a good rep for that, because you responded so fast!!!!! ( Like 5 secs after I posted it )
Again, Thanks!

Oreo

This post has been edited by Freakin' Oreo: 04 April 2011 - 04:39 PM

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• Posts: 45,424
• Joined: 27-December 08

## Re: Square Root without sqrt()

Posted 04 April 2011 - 04:39 PM

### #5 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Square Root without sqrt()

Posted 04 April 2011 - 04:50 PM

macosxnerd101, on 04 April 2011 - 06:35 PM, said:

You can use the definition of: e^(ln(x)/2), which will calculate sqrt(x). e^ln(x) comes out to x, and x^(1/2) is the square root of x.

That's hilarious.

Not incorrect, but if the assignment was to calculate square root without using the sqrt function, the intent was probably to do it without using any math library functions, so using exp and log would hardly be in the "spirit" of the task.

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Square Root without sqrt()

Posted 04 April 2011 - 06:49 PM

You could just use a loop:

```get target number you want the square root of
double n = 0.01
while((n * n) < target number) {
n += 0.01 //depending on what precision you want
}

```

It's not efficient, but the computer will rip them off pretty quickly for target numbers less than 100.

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12324
• Posts: 45,424
• Joined: 27-December 08

## Re: Square Root without sqrt()

Posted 04 April 2011 - 07:03 PM

For an iterative method, the Newton-Rhapson method might be better. At this point, you can ignore the Calculus behind it, and simply use the iterative formula:

x0 = initial guess
xi = xi-1 - (xi-12 - numToTakeSqrtOf)/(2*xi-1)

Iterate a few times, and you should get a good approximation.

### #8 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: Square Root without sqrt()

Posted 04 April 2011 - 07:25 PM

or you could craftily use bitwise operators. i used this on one of the project Euler problems when i needed to take the square root of a large integer.
integer sqrt in c

This post has been edited by ishkabible: 04 April 2011 - 07:26 PM

### #9 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Square Root without sqrt()

Posted 05 April 2011 - 07:22 AM

There are various ways to find square roots. Some of course are more efficient than others. But you should recognize that sometimes computation speed isn't your primary concern. Sometimes programming speed is more important. Sometimes you need a solution for a difficult calculation and no "efficient" solution exists (or you just can't find it). But you may still be able to take advantage of the computer's ability to iterate quickly through many calculations. If you can:
1- estimate an initial solution
2 -find a way to adjust the estimate to reduce the error on the next iteration
then you can write an iterative solution to the problem.

For square roots it is very easy to find an iterative solution. Here's one way, in pseudocode:

```input val (number whose squareroot you want) and epsilon (acceptable error margin)

root <=== 1
diff <=== val
while diff > epsilon
root <=== 0.5 * (root + val/root)
diff <=== val - (root*root)
if diff < 0
diff <=== -diff   // this is just getting the absolute value of the difference
output root

```

As you can see, coding this is trivial.

### #10 masoug

Reputation: 22
• Posts: 181
• Joined: 30-December 09

## Re: Square Root without sqrt()

Posted 05 April 2011 - 04:19 PM

Or you could use: pow(number, 0.5)