Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/sh

# We need to remove the "cache", else things are not regenerated properly
rm -rf autom4te.cache

# for warnings, add: -v -W all
autoreconf -i -s
# aclocal && libtoolize && autoconf && autoheader && automake -a

cat >doc/version.texi <<EOF
@set UPDATED 19 January 2038
@set UPDATED-MONTH January 2038
@set EDITION 12.35
@set VERSION 12.35
EOF
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Matches multiple files with brace expansion notation
[*.{c,h,cc}]
charset = utf-8
indent_style = tab
indent_size = 8
trim_trailing_whitespace = true

8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Makefile.in
build/
.gdbinit
.hg/
.hgignore
.hgtags

*/Makefile.in
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ MPZ_OBJECTS = mpz/abs$U.lo mpz/add$U.lo mpz/add_ui$U.lo \
mpz/millerrabin$U.lo mpz/mod$U.lo mpz/mul$U.lo mpz/mul_2exp$U.lo \
mpz/mul_si$U.lo mpz/mul_ui$U.lo \
mpz/n_pow_ui$U.lo mpz/neg$U.lo mpz/nextprime$U.lo \
mpz/out_raw$U.lo mpz/out_str$U.lo mpz/perfpow$U.lo mpz/perfsqr$U.lo \
mpz/nthprime_ui$U.lo mpz/out_raw$U.lo mpz/out_str$U.lo \
mpz/perfpow$U.lo mpz/perfsqr$U.lo \
mpz/popcount$U.lo mpz/pow_ui$U.lo mpz/powm$U.lo mpz/powm_sec$U.lo \
mpz/powm_ui$U.lo mpz/primorial_ui$U.lo \
mpz/pprime_p$U.lo mpz/random$U.lo mpz/random2$U.lo \
Expand Down
5 changes: 5 additions & 0 deletions doc/gmp.texi
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,11 @@ practical purposes it's adequate, the chance of a composite passing will be
extremely small.
@end deftypefun

@deftypefun void mpz_nthprime_ui (mpz_t @var{rop}, unsigned long int @var{n})
@cindex Nth prime function
Set @var{rop} to the @var{n}th prime. @var{rop} will be 2 if @var{n} is zero.
@end deftypefun

@c mpz_prime_p not implemented as of gmp 3.0.

@c @deftypefun int mpz_prime_p (const mpz_t @var{n})
Expand Down
3 changes: 3 additions & 0 deletions gmp-h.in
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ __GMP_DECLSPEC void mpz_neg (mpz_ptr, mpz_srcptr);
#define mpz_nextprime __gmpz_nextprime
__GMP_DECLSPEC void mpz_nextprime (mpz_ptr, mpz_srcptr);

#define mpz_nthprime_ui __gmpz_nthprime_ui
__GMP_DECLSPEC void mpz_nthprime_ui (mpz_ptr, unsigned long int);

#define mpz_out_raw __gmpz_out_raw
#ifdef _GMP_H_HAVE_FILE
__GMP_DECLSPEC size_t mpz_out_raw (FILE *, mpz_srcptr);
Expand Down
66 changes: 66 additions & 0 deletions mpz/nthprime_ui.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* mpz_nthprime_ui(p, n) - compute the nth prime and store it in p.

Copyright 2019 Free Software Foundation, Inc.

Contributed to the GNU project by Seth Troisi

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.

or

* the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any
later version.

or both in parallel, as here.

The GNU MP Library 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 copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */

#include "gmp-impl.h"
#include "longlong.h"

/* Enhancements:

- Use sieve for smallish n
- Use precomputed lookup values as started point
- Implement a more modern algorithm */

void
mpz_nthprime_ui (mpz_ptr p, unsigned long n)
{
/* UNDERSTAND: this condition appears for mpz_primorial_ui */
ASSERT (n <= GMP_NUMB_MAX);

/* Allocate 1 LIMBS */
MPZ_NEWALLOC (p, 1);

PTR (p)[0] = 1;
SIZ (p) = 1;

if (n <= 1)
{
/* nth_prime(p, 0) = 2 */
PTR (p)[0] = 2;
return;
}

/* Simple proof of concept implementation, soon to be replaced. */
while (n-- > 0)
{
mpz_nextprime (p, p);
}
}
3 changes: 2 additions & 1 deletion tests/mpz/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ check_PROGRAMS = reuse t-addsub t-cmp t-mul t-mul_i t-tdiv t-tdiv_ui t-fdiv \
t-fac_ui t-mfac_uiui t-primorial_ui t-fib_ui t-lucnum_ui t-scan t-fits \
t-divis t-divis_2exp t-cong t-cong_2exp t-sizeinbase t-set_str \
t-aorsmul t-cmp_d t-cmp_si t-hamdist t-oddeven t-popcount t-set_f \
t-io_raw t-import t-export t-pprime_p t-nextprime t-remove t-limbs
t-io_raw t-import t-export t-pprime_p t-nextprime t-remove t-limbs \
t-nthprime_ui

TESTS = $(check_PROGRAMS)

Expand Down
135 changes: 135 additions & 0 deletions tests/mpz/t-nthprime_ui.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* Test mpz_nthprime_ui.

Copyright 2019 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/. */

#include <stdio.h>
#include <stdlib.h>

#include "gmp-impl.h"
#include "tests.h"

/* Verify nth_prime(n) = want and isprime(want). */
void
check_one (long n, long want)
{
mpz_t got;
mpz_init (got);

mpz_nthprime_ui (got, n);
MPZ_CHECK_FORMAT (got);

if (mpz_cmp_si (got, want) != 0)
{
printf ("mpz_nthprime wrong\n");
printf (" n=%lu\n", n);
printf (" got=");
mpz_out_str (stdout, 10, got);
printf ("\n");
printf (" want=%lu\n", want);
abort ();
}

if (!mpz_probab_prime_p (got, 25))
{
printf ("mpz_nthprime_ui not prime\n");
printf (" n=%lu\n", n);
printf (" p=");
mpz_out_str (stdout, 10, got);
printf ("\n");
abort ();
}

mpz_clear (got);
}

void
check_data (void)
{
static const struct
{
int n;
long want;
} data[] = {
{ 1, 2 },
{ 2, 3 },
{ 3, 5 },
{ 4, 7 },
{ 10, 29 },
{ 25, 97 },
{ 100, 541 },
{ 1000, 7919 },
{ 3141, 28843 },
};

for (int i = 0; i < numberof (data); i++)
{
check_one (data[i].n, data[i].want);
}
}

/* check nthprime(0) is correct */
void
check_zero (void)
{
mpz_t test;
mpz_init (test);

mpz_nthprime_ui (test, 0);
if (mpz_cmp_si (test, 2))
{
printf ("mpz_nthprime_ui(0) != 2\n");
printf (" got ");
mpz_out_str (stdout, 10, test);
printf ("\n");
abort ();
}

MPZ_CHECK_FORMAT (test);

mpz_clear (test);
}

/* check small n's. */
void
check_small (void)
{
mpz_t test;
mpz_init_set_ui (test, 1);

for (int i = 1; i < 1000; i++)
{
mpz_nextprime (test, test);

check_one (i, mpz_get_ui (test));
}

mpz_clear (test);
}

int
main (int argc, char **argv)
{
tests_start ();

check_zero ();
check_small ();
check_data ();

tests_end ();
exit (0);
}