How do you write your own function for finding the most accurate square root of an integer?
After googling it, I found this (archived from its original link), but first, I didn't get it completely, and second, it is approximate too.
Assume square root as nearest integer (to the actual root) or a float.
In general the square root of an integer (like 2, for example) can only be approximated (not because of problems with floating point arithmetic, but because they're irrational numbers which can't be calculated exactly).
Of course, some approximations are better than others. I mean, of course, that the value 1.732 is a better approximation to the square root of 3, than 1.7
The method used by the code at that link you gave works by taking a first approximation and using it to calculate a better approximation.
This is called Newton's Method, and you can repeat the calculation with each new approximation until it's accurate enough for you.
In fact there must be some way to decide when to stop the repetition or it will run forever.
Usually you would stop when the difference between approximations is less than a value you decide.
EDIT: I don't think there can be a simpler implementation than the two you already found.
A simple solution that can deal with float square root and arbitrary precision using binary search
coded in ruby
Calculate square root with arbitrary precision in Python
Output:
The first thing comes to my mind is: this is a good place to use Binary search (inspired by this great tutorials.)
To find the square root of
vaule
,we are searching thenumber
in(1..value)
where the predictor is true for the first time. The predictor we are choosing isnumber * number - value > 0.00001
.It's a common interview question asked by Facebook etc. I don't think it's a good idea to use the Newton's method in an interview. What if the interviewer ask you the mechanism of the Newton's method when you don't really understand it?
I provided a binary search based solution in Java which I believe everyone can understand.
You can test my code here: leetcode: sqrt(x)
There is an algorithm that I studied in school that you can use to compute exact square roots (or of arbitrarily large precision if the root is an irrational number). It is definitely slower than Newton's algorithms but it is exact. Lets say you want to compute the square root of 531.3025
First thing is you divide your number starting from the decimal point into groups of 2 digits:
{5}{31}.{30}{25}
Then:
1) Find the closest square root for first group that is smaller or equal to the actual square root of first group: sqrt({5}) >= 2. This square root is the first digit of your final answer. Lets denote the digits we have already found of our final square root as B. So at the moment B = 2.
2) Next compute the difference between {5} and B^2: 5 - 4 = 1.
3) For all subsequent 2 digit groups do the following:
Multiply the remainder by 100, then add it to the second group: 100 + 31 = 131.
Find X - next digit of your root, such that 131 >=((B*20) + X)*X. X = 3. 43 * 3 = 129 < 131. Now B = 23. Also because you have no more 2-digit groups to the left of decimal points, you have found all integer digits of your final root.
4)Repeat the same for {30} and {25}. So you have:
{30} : 131 - 129 = 2. 2 * 100 + 30 = 230 >= (23*2*10 + X) * X -> X = 0 -> B = 23.0
{25} : 230 - 0 = 230. 230 * 100 + 25 = 23025. 23025 >= (230 * 2 * 10 + X) * X -> X = 5 -> B = 23.05
Final result = 23.05.
The algorithm looks complicated this way but it is much simpler if you do it on paper using the same notation you use for "long division" you have studied in school, except that you don't do division but instead compute the square root.