nth_prime

Seth Troisi braintwo at gmail.com
Sat May 18 23:14:24 UTC 2019


"An mpz_nthprime might be cute, but is almost certainly impractical for
anything but small n." --https://gmplib.org/projects.html

I thought this was a fun task so I coded it up. I've done it in three parts

1. Add a trivial implementation, docs, and test (github
<https://github.com/sethtroisi/libgmp/pull/7>, patch
<https://patch-diff.githubusercontent.com/raw/sethtroisi/libgmp/pull/7.patch>
)
2. Use gmp_nextprime which is internal and much faster (github
<https://github.com/sethtroisi/libgmp/pull/8>, patch
<https://patch-diff.githubusercontent.com/raw/sethtroisi/libgmp/pull/8.patch>
)
3. Use Legendre's Formula (github
<https://github.com/sethtroisi/libgmp/pull/9>)

I think 1. and 2. are as clean as I know how to make them (I don't see a
style guide so apology for formatting / style differences), 3. needs more
work  but I included it to demonstrate how this is more than a convenience
wrapper. Benchmarking 3, I find that nth_prime(1e9) takes a couple of
seconds.

--Seth
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nth_prime_1.patch
Type: text/x-patch
Size: 7915 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20190518/9f75357b/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nth_prime_2.patch
Type: text/x-patch
Size: 1726 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20190518/9f75357b/attachment-0001.bin>


More information about the gmp-devel mailing list