# integer square roots

Mensanator mensanator at aol.com
Thu Jul 23 20:30:34 EDT 2009

```On Jul 23, 7:17 pm, Mensanator <mensana... at aol.com> wrote:
> On Jul 23, 7:11 pm, Mensanator <mensana... at aol.com> wrote:
>
>
>
>
>
> > 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
> > (10000000000000000000000000000000000000000000000000000000000000000000000000­­000000000000000000000000267)
>
> > >>> gmpy.sqrt(p1)
>
> > mpz(100000000000000000000000000000000000000000000000000)
>
> Duh. I completely misread this. I thought you wanted the square root,
> not IF it had a square root that's an integer.
>
>
>
> >>> gmpy.sqrtrem(p1)
>
> (mpz(100000000000000000000000000000000000000000000000000), mpz(267))
>
> It has a non-zero remainder, so it's not an integer square root.

There's also

is_square(...)
is_square(x): returns 1 if x is a perfect square, else 0.
x must be an mpz, or else gets coerced to one.

```