math/mp-sqrt.c, math/pgen-granfrob.c: Publish `squarep' function.
[catacomb] / math / mp-sqrt.c
index bf19fe8..9530ff9 100644 (file)
@@ -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