3 ms·
That algorithm should be called square rootus horribilis. Let's say we're square rooting a 3-4 digit number (or you can multiply/divide out the exponent accord
by SamReidHughes 7y ago
That algorithm should be called square rootus horribilis.
Let's say we're square rooting a 3-4 digit number (or you can multiply/divide out the exponent accordingly).
I recommend starting with the (x + .5)^2 = x(x+1) + .25 shortcut, multiplied by 100, to be able to compute all multiple-of-five squares 5, 10, 15, 20, ..., 95. Then, from some multiple of 5 that is x, with k in {-2,-1,0,1,2}, use (x+k)^2 = x^2 + 2xk + k^2 to tweak that to a particular perfect square. E.g. 42^2 = 1764 = 1600+160+4. Now, we're square rooting a number, so find the two nearest perfect squares. Depending on how close you are to halfway between them, and depending on if you're in the mood for that sort of thing, poke it up by <0.25 to account for the quadratic factor of whatever you're square rooting (because (x+.5)^2 is 0.25 less than the average of x^2 and (x+1)^2), and then linearly interpolate between the two perfect squares. (You don't really have to calculate both neighboring squares to interpolate, because the difference is about 2x, and you probably already calculated that when you did the 2xk calculation.)
So for example, what's the square root of (types randomly) 5719? Why, 56 is 7*8, so 5625 is 75^2. We're like, 95 more than that, out of 150? So, 75.6 and change. Or more exactly, 94/151? That's like a +4% -.6% adjustment of .6 [1], so maybe 75.62. Or even closer, you might use 94.2/151, sure.
[1] (a+n%)/(b+m%) can be approximated (a/b) + n% - m%, for small percentages.