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

Thanks!

Oreo

# Square Root without sqrt()

Page 1 of 1## 9 Replies - 24712 Views - Last Post: 05 April 2011 - 04:19 PM

##
**Replies To:** Square Root without sqrt()

### #2

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

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

### #5

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

### #6

## Re: Square Root without sqrt()

Posted 04 April 2011 - 06:49 PM

You could just use a loop:

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

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

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

x

x

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

x

_{0}= initial guessx

_{i}= x_{i-1}- (x_{i-1}^{2}- numToTakeSqrtOf)/(2*x_{i-1})Iterate a few times, and you should get a good approximation.

### #8

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

integer sqrt in c

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

### #9

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

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

As you can see, coding this is trivial.

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

## Re: Square Root without sqrt()

Posted 05 April 2011 - 04:19 PM

Or you could use: pow(number, 0.5)

Page 1 of 1