integer square roots

Mensanator mensanator at aol.com
Thu Jul 23 20:11:54 EDT 2009


On Jul 23, 6:02 pm, timro21 <timr... at gmail.com> wrote:
> I wish to process billions of 100-digit numbers and test if each has
> an integer square root.  What's the most efficient way to do this?

Use gmpy.

>>> import gmpy
>>> help(gmpy.sqrt)

Help on built-in function sqrt in module gmpy:

sqrt(...)
    sqrt(x): returns the integer, truncated square root of x, i.e. the
    largest y such that x>=y*y. x must be an mpz, or else gets coerced
    to one; further, x must be >= 0.

>>> p = 10**100

>>> p1 = gmpy.next_prime(p)

>>> p1
mpz
(10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000267)

>>> gmpy.sqrt(p1)
mpz(100000000000000000000000000000000000000000000000000)





More information about the Python-list mailing list