9 Replies - 25166 Views - Last Post: 05 April 2011 - 04:19 PM Rate Topic: -----

#1 Freakin' Oreo   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • 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. :)
Was This Post Helpful? 1
  • +
  • -

#3 Freakin' Oreo   User is offline

  • New D.I.C Head

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

Re: Square Root without sqrt()

Posted 04 April 2011 - 04:37 PM

View Postmacosxnerd101, 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

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




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

Re: Square Root without sqrt()

Posted 04 April 2011 - 04:39 PM

Glad I could help! :)
Was This Post Helpful? 1
  • +
  • -

#5 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

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

Re: Square Root without sqrt()

Posted 04 April 2011 - 04:50 PM

View Postmacosxnerd101, 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. :clap:

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

#6 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#8 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 masoug   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1