ANN: pyprimes 0.1 released
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Feb 22 21:09:09 EST 2012
I am happy to announce the first public release of pyprimes, a pure-
Python module for generating prime numbers, primality tests, and prime
factorisation.
http://pypi.python.org/pypi/pyprimes/0.1.1a
With pyprimes, you can compare a number of algorithms for generating and
testing primes. It includes fast algorithms for lazily generating primes
on demand, such as the Croft Spiral, as well as terrible examples of
algorithms to avoid, such as Turner's Sieve (sometimes known as "the
Sleight on Eratosthenes").
Examples:
py> import pyprimes
py> pyprimes.isprime(3300454933)
True
py> pyprimes.factors(3300454934)
[2, 7, 14969, 15749L]
py> for p in pyprimes.primes():
... if p < 500000: continue
... if p > 500100: break
... print p
...
500009
500029
500041
500057
500069
500083
A note on performance:
Generating large prime numbers (hundreds of digits) is extremely
computationally intensive. A pure-Python solution like pyprimes is
unlikely to be suitable for such large numbers, and I make no claim that
it will break any world-records.
But many popular and common prime number generators can't even deal well
with *five* digit primes, slowing down to a crawl. pyprimes is in some
cases hundreds of times faster than such prime generators. pyprimes
includes examples of these algorithms so you can compare them for
yourself:
py> from time import time
py> def test(generator):
... t = time()
... for p in generator():
... if p > 10000: break
... return time() - t
...
py> test(pyprimes.primes)
0.013000965118408203
py> test(pyprimes.naive_primes1)
4.1489889621734619
--
Steven
More information about the Python-list
mailing list