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

Which is the fastest method for calculating exact square root of a integer of 200-500 digit number?

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

Problem

I wanted to know is there any algorithm / function / process through which I can calculate square root of a very large integer number. I wants to know current state of the research in this field.

No approximate methods please.

Solution

It seems a bit surprising that you did not find the Wikipedia article on integer square root where the Newton's algorithm is described in detail.

Here is the implementation in Python:

def integer_sqrt(n):
    """Compute the integer square root of n, or None if n is not a perfect square."""
    x = n // 2
    while True:
        y = (x + n // x) // 2
        if abs(x - y) < 2: break
        x = y
    return (x if x * x == n else None)


This takes about 0.018 seconds for a 1000 digit number on my MacBook Air. It's not clear why you're asking for the particular range between 200 and 500 digits, but I think you should be able to go on from here, especially if you take the time to read the Wikipedia page.

You may also be interested in the Rosetta Code integer roots page, where you can find fast implementations of integer root algorithms in many languages.

By the way, I just googled "integer square root" to find all of this, except I wrote the Python code.

Code Snippets

def integer_sqrt(n):
    """Compute the integer square root of n, or None if n is not a perfect square."""
    x = n // 2
    while True:
        y = (x + n // x) // 2
        if abs(x - y) < 2: break
        x = y
    return (x if x * x == n else None)

Context

StackExchange Computer Science Q#97272, answer score: 4

Revisions (0)

No revisions yet.