Copyright 2002, 2018-2019, 2022 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
The GNU MP Library test suite is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
- Test some big primes don't come back claimed to be composite. - Test some big composites don't come back claimed to be certainly prime. - Test some big composites with small factors are identified as certainly
composite. */
/* return 2 if prime, 0 if composite */ int
isprime (unsignedlong n)
{ if (n < 4) return (n & 2); if ((n & 1) == 0) return 0;
for (unsignedlong i = 3; i*i <= n; i+=2) if ((n % i) == 0) return 0;
return 2;
}
void
check_one (mpz_srcptr n, int want)
{ int got;
got = mpz_probab_prime_p (n, 25);
/* "definitely prime" (2) is fine if we only wanted "probably prime" (1) */ if ((got != want) && (got != want * 2))
{
printf ("mpz_probab_prime_p\n");
mpz_trace (" n ", n);
printf (" got =%d", got);
printf (" want=%d", want);
abort ();
}
}
void
check_pn (mpz_ptr n, int want)
{
check_one (n, want);
mpz_neg (n, n);
check_one (n, want);
}
/* expect certainty for small n */ void
check_small (void)
{
mpz_t n; long i;
mpz_init (n);
for (i = 0; i < 300; i++)
{
mpz_set_si (n, i);
check_pn (n, isprime (i));
}
mpz_clear (n);
}
void
check_composites (int count)
{ int i;
mpz_t a, b, n, bs; unsignedlong size_range, size;
gmp_randstate_ptr rands = RANDS;
staticconstchar * const composites[] = { "225670644213750121", /* n=61*C16, if D < 61, (n/D) = 1. */ "2386342059899637841", /* n=61*C17, if D < 61, (n/D) = 1. */ "1194649", /* A square, but strong base-2 pseudoprime, */ "12327121", /* another base-2 pseudoprime square. */ "18446744066047760377", /* Should trigger Fibonacci's test; */ "10323769", /* &3==1, Lucas' test with D=37; */ "1397419", /* &3==3, Lucas' test with D=43; */ "11708069165918597341", /* &3==1, Lucas' test with large D=107; */ "395009109077493751", /* &3==3, Lucas' test with large D=113. */
NULL
};
for (i = 0; composites[i]; i++)
{
mpz_set_str_or_abort (n, composites[i], 0);
check_one (n, 0);
}
for (i = 0; i < count; i++)
{
mpz_urandomb (bs, rands, 32);
size_range = mpz_get_ui (bs) % 13 + 1; /* 0..8192 bit operands */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.