Package: pyprimesieve
pyprimesieveMany primes, very fast. Usesprimesieve.primesieve, one of the fastest (if not the fastest) prime sieve implementaions available, is actively maintained by
Kim Walisch.It uses a segmented sieve of Eratosthenes with wheel factorization for a complexity ofO(nloglogn)operations.PerformanceRegarding primesieve for C++:primesieve generates the first 50,847,534 primes up to 10^9 in just 0.4 seconds on a single core of an Intel Core
i7-920 2.66GHz, this is about 50 times faster than an ordinary C/C++ sieve of Eratosthenes implementation and about
10,000 times faster than trial-division. primesieve outperforms [Kim’s] olderecprime(fastest from 2002 to 2010) by
about 30 percent and also substantially outperformsprimegenthe fastest sieve of Atkin implementation on the
web.For comparison, on an Intel Core i7 2GHz,pyprimesievepopulates an entire Python list of the first
50,847,534 primes in 1.40 seconds. It’s expected that a Python implementation would be slower than C++ but,
surprisingly, by only one second.pyprimesieveoutperforms all of the fastest prime sieving implementations for Python.Time (ms) to generate the all primes below one million and iterate over them in Python:algorithmtimepyprimesieve2.79903411865primesfrom2to13.1568908691primesfrom3to13.5800838470ambi_sieve16.1600112915rwh_primes238.7749671936rwh_primes148.5658645630rwh_primes52.0040988922sieve_wheel_3059.3869686127sieveOfEratosthenes59.4990253448ambi_sieve_plain161.740064621sieveOfAtkin232.724905014sundaram3251.194953918It can be seen here thatpyprimesieveis4.7 times fasterthan the fastest Python alternative usingNumpyand13.85 times fasterthan the fastest pure Python sieve.All benchmark scripts and algorithms are available for reproduction. Prime sieve algorithm implementations were taken
fromthis discussion on SO.Functionsprimes(n): List of prime numbers up ton.primes(start, n): List of prime numbers fromstartup ton.primes_sum(n): The summation of prime numbers up ton. The optimal number of threads will be determined for the
given number and system.primes_sum(start, n): The summation of prime numbers fromstartup ton. The optimal number of threads will be
determined for the given numbers and system.primes_nth(n): The nth prime number.factorize(n): List of tuples in the form of (prime, power) for the prime factorization ofn.InstallationpipinstallpyprimesieveNOTE: To enable the parallelized version of prime summation, you must use a compiler that supports OpenMP. You may
need to pass a valid compiler as an environment variable.TestingAfter installation, you can make sure everything is working by running the following inside the project root folder,pythontestsLicense“Modified BSD License”. See LICENSE for details. Copyright Jared Suttles, 2015.Changes0.1.6 (2015-07-07)Python 3 supportNo longer assumeg++insetup.pybut rather tries installing with OpenMP and retries if that fails0.1.5 (2015-07-05)added to pypi0.1.4 (2013-08-06)Parallel prime summation is now a classThreads calling callback method are NOT synchronized so faster on large inputUpdated README in bench to reflect new timings (small input is slightly worse but large is twice as fast)0.1.3 (2013-08-05)Fixed compilation warningsextern “C”Added more information regarding installation to README; updated docstring0.1.2 (2013-07-12)primes_nth uses callback class instead of creating any PyObjects in order to call primesCaused a memory leak and was unnecessary0.1.1 (2013-07-12)Distutils setup.py: got multithreaded summation compiling with OpenMPRemoved all code to manually compile executableCC and CXX environment variables set to avoid clang (and other non-openmp compilers) useReplaced executable code with parallelized function in the actual extensionRewrote primes_sum and primes_nth Python functions in C++ and removed Python script completely0.1.0 (2013-07-06)pyprimesieve initial