## Curious formula generating all digits of square root numbers

5 stars based on
55 reviews

How do you calculate the square root of a number? Well, now-a-days it's easy--just use sqrt. But suppose you don't have that function, how else can you do it? Here are some various ways of doing it, from Newton's Method to the paper-and-pencil "long square root" method that I was taught in grade school which is akin to long division. SQRT Here's the best way but least fun. It's here for timing purposes: Or just use [:: Newton's Method This is an iterative process that takes an initial guess and successively makes it better using the recursion: The tricky part is knowing when to stop pick some epsilon and determining the initial guess--any value will do, but some converge faster.

A lot has been written as to the best initial guess; the algorithm I use here is to use a number whose high-bit position is half that of the original number.

I learned this method in grade school, but it seems to be a forgotten technique. In assembly language, working in base 2, this is a blazingly fast and very compact algorithm involving just shifts, adds and comparisons. As a refresher and to get a common terminology, in long division, you have the divisor, dividend and quotient. Each step involves bringing down the next digit from the dividend and adding it to the running dividendthen finding the maximum digit Dwhich multiplied by the divisor is less than the running dividend.

That digit D then becomes part of the quotient. In long square root the method is almost identical except that you bring down pairs of digits and the divisor analog is a varying, more complex value. The square root analog to the divisor, dividend and quotient terms I'll call cross termsquareand root.

Each step involves bringing down the next 2 digits from the square and adding it to the running squarethen finding the maximum digit D which multiplied by the cross term see below is less than the running square. That digit D then becomes part recurrence relation for the digits of the integer square root in binary the root. The cross term part changes at each step. AM It is a pity that this method can not easily be extended to arbitrary roots, such as the cubic root.

Note that the method boils down to: Long Square Root -- reals The above method can easily handle real numbers--just like in long division, when you reach the decimal point just keep bringing down 0's but for square root you bring down pairs of 0's. A tricky part is keeping track of where the decimal point should go.

This function will return as many decimal places as its input, to wit, supplying 2. Should use powers of 2 for this! I was reading [ 2 ] Wayback Machine Web Archive about some obscure code that calculates an approximation to the inverse square root of a number i.

What particularly interested me was the method of calculation, which I reproduce here in Tcl code with some simplifications to make the code a bit more readable: Repeating the last calculation step will produce better approximations: There are for example tons of results that the asymptotic complexity of computing some function e.

As for the magical starting approximation which is analysed in a paper [ 3 ] linked to by the text linked to abovethe idea is if recurrence relation for the digits of the integer square root in binary simplifies heavily to multiply the exponent of the floating point number by The trick relies on the fact that the float i is in IEEE format, ii is 32 bits long but changing the magic constant 0x5fdf suffices for adapting the trick to other sizesand iii has the same endianness as the integers. See also [ 4 ].

Supposing that **recurrence relation for the digits of the integer square root in binary** does not exist? I can't resist offering the following trivial alternative: What if you do not have exp? Getting started What is Tcl? Tcl in the wild Major applications.