wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> microsoft >> Efficient Algo for calculating SQUARE ROOT
(Message started by: solid on Sep 4th, 2011, 11:21pm)

Title: Efficient Algo for calculating SQUARE ROOT
Post by solid on Sep 4th, 2011, 11:21pm
Does anyone know efficient and correct algorithm for calculating the Square Root of a number. I searched lot of interview, couldn't find any convincing method.

Pseudo code will be appreciated.

Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by solid on Sep 4th, 2011, 11:23pm
is it the correct pseudo/formula to calculate square root:

repeat:
x = (x + a/x)/2;



Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by Grimbal on Sep 5th, 2011, 1:09am
That is probably the simplest and is still reasonably fast.

If floating point division is not available, or too slow, you can do a binary search.

There is also a method using only integer additions and comparisons to compute the mantissa.

If you have a fast log and exp, you can do exp(log(x)/2).

Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by solid on Sep 5th, 2011, 9:27am
Could you please elaborate a bit? Or pseudo code?

Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by fizyka on Dec 30th, 2011, 2:08pm
So standard math.h from C language isn't enough fast?
For what You need faster code? I dont follow.

Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by towr on Dec 31st, 2011, 2:40am
The idea of a puzzle is to figure out how to solve it, not to look up the answer in the back of the book.

Title: Re: Efficient Algo for calculating SQUARE ROOT
Post by webtasarim on Oct 20th, 2013, 5:33pm
Set the factor F to 16
If N < 1 Then N = 1 / N
Let X = N
Let LastX = X
Calculate X = X / F
If X*X > N then resume to step 10
Reduce factor F such that it approaches 1
Let X = LastX / F
Go to step 6
If |X*X - N| > Tolerance And |F - 1| > Tolerance then resume at step 4
Return X as the square root of N, or return 1/X if the original N was less than 1



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board