/* -*-c-*-
*
- * $Id: pfilt.c,v 1.1 1999/12/22 15:49:39 mdw Exp $
+ * $Id: pfilt.c,v 1.4 2000/10/08 12:14:57 mdw Exp $
*
* Finding and testing prime numbers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: pfilt.c,v $
+ * Revision 1.4 2000/10/08 12:14:57 mdw
+ * Remove vestiges of @primorial@.
+ *
+ * Revision 1.3 2000/08/15 21:44:27 mdw
+ * (pfilt_smallfactor): New function for doing trial division the hard
+ * way.
+ *
+ * (pfilt_create): Use @mpx_udivn@ for computing residues, for improved
+ * performance.
+ *
+ * Pull the `small prime' test into a separate function, and do it
+ * properly.
+ *
+ * Revision 1.2 2000/06/17 11:54:27 mdw
+ * Use new MP memory management functions.
+ *
* Revision 1.1 1999/12/22 15:49:39 mdw
* Renamed from `pgen'. Reworking for new prime-search system.
*
/*----- Header files ------------------------------------------------------*/
#include "mp.h"
-#include "mpmont.h"
+#include "mpint.h"
#include "pfilt.h"
#include "pgen.h"
#include "primetab.h"
/*----- Main code ---------------------------------------------------------*/
+/* --- @smallenough@ --- *
+ *
+ * Arguments: @mp *m@ = integer to test
+ *
+ * Returns: One of the @PGEN@ result codes.
+ *
+ * Use: Assuming that @m@ has been tested by trial division on every
+ * prime in the small-primes array, this function will return
+ * @PGEN_DONE@ if the number is less than the square of the
+ * largest small prime.
+ */
+
+static int smallenough(mp *m)
+{
+ static mp *max = 0;
+ int rc = PGEN_TRY;
+
+ if (!max) {
+ max = mp_fromuint(MP_NEW, MAXPRIME);
+ max = mp_sqr(max, max);
+ max->a->n--; /* Permanent allocation */
+ }
+ if (MP_CMP(m, <, max))
+ rc = PGEN_DONE;
+ return (rc);
+}
+
+/* --- @pfilt_smallfactor@ --- *
+ *
+ * Arguments: @mp *m@ = integer to test
+ *
+ * Returns: One of the @PGEN@ result codes.
+ *
+ * Use: Tests a number by dividing by a number of small primes. This
+ * is a useful first step if you're testing random primes; for
+ * sequential searches, @pfilt_create@ works better.
+ */
+
+int pfilt_smallfactor(mp *m)
+{
+ int rc = PGEN_TRY;
+ int i;
+ size_t sz = MP_LEN(m);
+ mpw *v = mpalloc(m->a, sz);
+
+ /* --- Fill in the residues --- */
+
+ for (i = 0; i < NPRIME; i++) {
+ if (!mpx_udivn(v, v + sz, m->v, m->vl, primetab[i])) {
+ if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
+ rc = PGEN_DONE;
+ else
+ rc = PGEN_FAIL;
+ }
+ }
+
+ /* --- Check for small primes --- */
+
+ if (rc == PGEN_TRY)
+ rc = smallenough(m);
+
+ /* --- Done --- */
+
+ mpfree(m->a, v);
+ return (rc);
+}
+
/* --- @pfilt_create@ --- *
*
* Arguments: @pfilt *p@ = pointer to prime filtering context
{
int rc = PGEN_TRY;
int i;
- mp *r = MP_NEW;
- mpw qw;
- mp q;
+ size_t sz = MP_LEN(m);
+ mpw *v = mpalloc(m->a, sz);
/* --- Take a copy of the number --- */
/* --- Fill in the residues --- */
- mp_build(&q, &qw, &qw + 1);
for (i = 0; i < NPRIME; i++) {
- qw = primetab[i];
- mp_div(0, &r, m, &q);
- p->r[i] = r->v[0];
+ p->r[i] = mpx_udivn(v, v + sz, m->v, m->vl, primetab[i]);
if (!p->r[i] && rc == PGEN_TRY) {
if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
rc = PGEN_DONE;
}
}
+ /* --- Check for small primes --- */
+
+ if (rc == PGEN_TRY)
+ rc = smallenough(m);
+
/* --- Done --- */
- mp_drop(r);
+ mpfree(m->a, v);
return (rc);
}
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Done --- */
/* --- Multiply the big number --- */
{
- mp *d = mp_create(MP_LEN(q->m) + 2);
+ mp *d = mp_new(MP_LEN(q->m) + 2, q->m->f);
mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
mpx_uaddn(d->v, d->vl, a);
- d->f = q->m->f;
if (p == q)
mp_drop(p->m);
mp_shrink(d);
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Finished --- */
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Done --- */