23

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?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • I used [primality](https://pypi.org/project/primality/) today to check whether a 128-byte number is prime. It's quite fast, producing the answer seemingly instantaneously. – Matthias Braun Oct 05 '21 at 17:28

6 Answers6

25

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
Falko
  • 17,076
  • 13
  • 60
  • 105
14

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/

John Y
  • 14,123
  • 2
  • 48
  • 72
casevh
  • 11,093
  • 1
  • 24
  • 35
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.

voithos
  • 68,482
  • 12
  • 101
  • 116
2

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.

JustinDanielson
  • 3,155
  • 1
  • 19
  • 26
  • 3
    I am explicitly not looking for implementations. I have already implemented the code, but I generally prefer loading a function from a module that has been reviewed than writing it myself, especially if it is something well-knowen like Miller-Rabin. – Martin Thoma Jun 11 '12 at 12:15
0

Primes-Library-Python is an in development python library. Good for basic functions and very fast for the bigger numbers.

Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
MagikCow
  • 863
  • 8
  • 13
0

I created a library with many, many functions (see the README), but it's most important is that it includes Alpertons ECM library so you can factor with Alperton's Engine (Even under Termux under Android) from ipython3:

https://github.com/oppressionslayer/primalitytest/

Just watch the video at the beginning of the README and see how easy it is to get it working with Alperton's C library.

to use it, just use from sfactorint import p2ecm from the primality directory. To get access to Alperton's ECM, follow these steps:

cd calculators
make
cd ..

And that's it, sfactorint then uses Alpertons ECM SIQS for factorization. I plan to make a pip install that does the make step automatically, so keep watching if you interested in it.

Here's a sample 60 digit number factor from it, i do the same one on an Android in the video at about the same speed:

In [22]: import time   
    ...: start = time.time()   
    ...: print(p2ecm(632459103267572196107100983820469021721602147490918660274601))   
    ...: end = time.time()   
    ...: print(end-start)    
    ...:                                                                                                                                                       
[650655447295098801102272374367, 972033825117160941379425504503]
5.197108030319214
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24