Skip to main content

An understandable prime sieve implementation in numpy or pure python.

Project description

Prime Sieve

https://img.shields.io/pypi/v/prime_sieve.svg https://img.shields.io/travis/mCodingLLC/prime_sieve.svg Documentation Status

An understandable prime sieve implementation in numpy or pure python. The focus is providing a sieve that is easy to understand rather than the absolute fastest implementation. Though the numpy implementation is reasonably quick, being able to compute the first 100 million primes in 30 seconds on my mid-tier laptop.

Usage

# Use a numpy or pure python implementation
from prime_sieve.array import PrimeArraySieve
# from prime_sieve.list import PrimeListSieve

sieve = PrimeArraySieve()
# sieve = PrimeListSieve()

print(sieve.nth_prime(0)) # 2
print(sieve[4]) #  7

print(sieve[:100]) # [2, 3, ..., 541]
print(sieve[1:6]) # [3, 5, 7, 11, 13]

print(86*97 in sieve) # False
print(sieve.is_prime(2 ** 13 - 1)) # True

# ranges are like python ranges, inclusive start, exclusive stop
print(sieve.primes_in_range(10, 20)) # [11, 13, 17, 19]
print(sieve.primes_in_range(10, 19)) # [11, 13, 17]

print(sieve.count_primes_in_range(3, 7)) # 2
print(sieve.count_primes_in_range(3, 8)) # 3

print(sieve.next_prime_greater_than(100)) # 101
print(sieve.next_prime_greater_than(101)) # 103

print(sieve.prev_prime_less_than(8)) # 7
print(sieve.prev_prime_less_than(7)) # 5

print(sieve.count_primes_less_or_equal(10 ** 7)) # 664579

for p in sieve.iter_all_primes(): # infinite loop
    print(p)

# see sieve internals
print(len(sieve)) # how many primes have currently been computed
print(sieve.primes) # read-only view of already computed primes

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2021-05-02)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prime_sieve-0.1.4.tar.gz (14.5 kB view hashes)

Uploaded Source

Built Distribution

prime_sieve-0.1.4-py2.py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page