Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a library for prime-related functions exist for Python?

Tags:

I've just implemented the Miller-Rabin-Test and a simple function for factorizing numbers. Both could be done better and at least the Miller-Rabin-Test is well-known.

So could you please tell me if a Python-Library, that implements such common prime functions exists or why no such library exists?

like image 365
Martin Thoma Avatar asked Jun 11 '12 05:06

Martin Thoma


People also ask

Is there a library for prime numbers in Python?

primePy is that library of Python which is used to compute operations related to prime numbers. It will perform all the functions in less time with the help of the functions of this primePy module.

How do you write a prime function in Python?

We could have used the range, range(2,num//2) or range(2,math.floor(math.sqrt(num)+1)) . The latter range is based on the fact that a composite number must have a factor less than or equal to the square root of that number. Otherwise, the number is prime.

Is Python a prime SymPy?

SymPy is a python module which contains some really cool prime number related library functions.


2 Answers

I just discovered isprime from the SymPy package:

import sympy print sympy.isprime(10) 

Output:

False 

Not to confuse with prime, which returns the n-th prime number:

import sympy print sympy.prime(10) 

Output:

29 
like image 136
Falko Avatar answered Sep 17 '22 12:09

Falko


gmpy2 supports a variety of pseudoprime tests. The Miller-Rabin test is available as gmpy2.is_strong_prp().

gmpy2 does not have any factorization code yet.

Disclaimer: I'm the maintainer of gmpy2. The primality tests are based on code from http://sourceforge.net/projects/mpzprp/files/

like image 21
casevh Avatar answered Sep 17 '22 12:09

casevh