问题
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?
回答1:
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/
回答2:
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
回答3:
I do not think that there exists such a module dedicated to prime functions in the standard library, but of course there are plenty of people who have written primality tests and such.
One library that is geared towards multiple-precision arithmetic, but which has several functions for primes (such as is_prime()
and next_prime()
) is GMPY2. The documentation is also available.
回答4:
If you're looking for implementations of algorithms, check out Rosetta Code. The website has many implementations in Python. You could definitely piece together your own library for your personal need.
回答5:
Prime-Number-Python-Library is an in development python library. Good for basic functions and very fast for the bigger numbers.
来源:https://stackoverflow.com/questions/10974805/does-a-library-for-prime-related-functions-exist-for-python