X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/1b420890ac399d1844ab6c4fe0e0e22ba8fbe3ad..0e1199ac5a1d8410608d488aaa787fb207fc3746:/math/mp-sqrt.c diff --git a/math/mp-sqrt.c b/math/mp-sqrt.c index bf19fe8e..9530ff99 100644 --- a/math/mp-sqrt.c +++ b/math/mp-sqrt.c @@ -126,6 +126,24 @@ mp *mp_sqrt(mp *d, mp *a) return (d); } +/* --- @mp_squarep@ --- * + * + * Arguments: @mp *n@ = an integer + * + * Returns: Nonzero if and only if @n@ is a perfect square, i.e., + * %$n = a^2$% for some rational integer %$a$%. + */ + +int mp_squarep(mp *n) +{ + mp *t = MP_NEW; + int rc; + + if (MP_NEGP(n)) return (0); + t = mp_sqrt(t, n); t = mp_sqr(t, t); + rc = MP_EQ(t, n); mp_drop(t); return (rc); +} + /*----- Test rig ----------------------------------------------------------*/ #ifdef TEST_RIG