X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb/blobdiff_plain/6af2607b63ce649a919513949d5f8a8deb2e6663..d2679863cf27b0812a4b88397be1ebf0b1319305:/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