HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Where can I find an original reference for this integer square root algorithm

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thisreferencecansquareoriginalwherealgorithmforrootfind

Problem

As an exercise, I converted an old method I learned for calculating square roots on a rotary decimal hand calculator to binary.

I'm sure this is not original; can anyone provide a reference?

// All arithmetic is truncating unsigned integer arithmetic
// Actual implementation uses bit shifts
// Indention level defines statement scope

def sqrt(r)                         // r is an unsigned integer
    residue = r                     // residue will be the 'remainder'
    root = 0                        // root will be the integer square root (floor(r**0.5))
    onebit = 2**(bitlength(r)-2)    // onebit is a moving 'bitpicker'
    while onebit > r                
        onebit = onebit / 4         // find highest bitpicker less than r
    while onebit > 0 
        x = root + onebit           // Current root plus onebit
        if residue >= x             // Room to subtract?
            residue = residue - x   // Yes - deduct from residue
            root = x + onebit       // and step root
        root = root / 2             // Slide evolving root 1 bit down the residue
        onebit = onebit / 4         // Slide the bitpick 1 bit down the root
    assert(root**2 + residue == r)
    assert((root+1)**2 > r)
    return root, residue

Solution

The wikipedia article on Methods of computing square roots: base 2 presents a strikingly similar snippet of C-code [a], but the link to the source is dead. Let's try to do better.

The snippets from both wikipedia and the question are very similar to Martin Guy's widely circulated C implementation [b]. which contains a comment: From a book on programming abaci by Mr C. Woo. Search engine use suggests this is [1] - one edition seems to be from 1930; it has been reprinted, e.g. [2].

The age of the method is bound to be on the order of two millenniums - the original reference might be difficult to obtain.

A purportedly[z] very similar Algorithm has been presented by TIJ Rolfe, who in turn refers to [3].

  • Woo, C.C. The Fundamental Operations in Bead Arithmetic. How to Use the Chinese Abacus, China Lace Co., Ltd., Hong Kong



  • Kwa Tak Ming, C. C. Woo, The Fundamental Operations in Bead Arithmetic. How to Use the Chinese Abacus, Literary Licensing, LLC, United States, 2012, 9781258466855



  • Hart, W.L. "College Algebra" 4th ed., Boston, 1955, p.424

Context

StackExchange Computer Science Q#52683, answer score: 9

Revisions (0)

No revisions yet.