equivalent of primePy.isaprime?

12 views
Skip to first unread message

Paul Mansfield

unread,
Dec 5, 2024, 7:30:46 AM12/5/24
to mpmath
Hi,
I was experimenting with primes, as you do, and used primePy to test a value, something like the code snippet below (typed here from memory)

I couldn't see an equivalent of this in mpmath. Has someone witten an optimised checker by any chance?

thanks!


from primePy import primes

  if primes.check(some_variable):
      print(f'{some_variable} is prime')

Sergey B Kirpichev

unread,
Dec 5, 2024, 7:40:46 AM12/5/24
to mpm...@googlegroups.com
Primality testing rather outside of the mpmath scope (floating-point
arithmetic).

You should try something like gmpy2, python-flint, etc.

Paul Mansfield

unread,
Dec 5, 2024, 7:50:36 AM12/5/24
to mpmath
thanks. I should add I am not a mathematician so apologies if I am vague

my concern is that if the number I am testing has too many digits, then using one of libraries like primePy.isaprime or sympy will break and give errors or the wrong answers, hence wondering if mpmath has a ready made solution I can cargo-cult.

OTOH, I am not going to be dealing with truly huge prime numbers for now, so maybe I should simply use a lookup table giving me all primes up to a few hundred digits?

thanks

Oscar Benjamin

unread,
Dec 5, 2024, 9:19:23 AM12/5/24
to mpm...@googlegroups.com
On Thu, 5 Dec 2024 at 12:51, Paul Mansfield <pa...@mansfield.co.uk> wrote:
>
> thanks. I should add I am not a mathematician so apologies if I am vague
>
> my concern is that if the number I am testing has too many digits, then using one of libraries like primePy.isaprime or sympy will break and give errors or the wrong answers, hence wondering if mpmath has a ready made solution I can cargo-cult.

What is it that makes you think that something would break if you have
too many digits? Why would you expect that copying some code from
mpmath instead would solve that problem?

> OTOH, I am not going to be dealing with truly huge prime numbers for now, so maybe I should simply use a lookup table giving me all primes up to a few hundred digits?

That lookup table would be very large (too large to fit into computer memory).

--
Oscar

Sergey B Kirpichev

unread,
Dec 5, 2024, 9:33:02 AM12/5/24
to mpm...@googlegroups.com
On Thu, Dec 05, 2024 at 04:50:36AM -0800, Paul Mansfield wrote:
> using one of libraries like primePy.isaprime or sympy will break and give
> errors or the wrong answers

In general, for large numbers - probabilistic tests are used. The gmpy2
now has
https://gmpy2.readthedocs.io/en/latest/mpz.html#gmpy2.mpz.is_probab_prime
so, you can distinguish cases when primality is for sure and where the
number is only *probable* prime.
Reply all
Reply to author
Forward
0 new messages