Skip to main content

Prime Sieve

https://img.shields.io/pypi/v/prime_sieve.svg https://travis-ci.com/mCodingLLC/prime_sieve.svg?branch=master 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.

This library implements a version of the segmented sieve of Eratosthenes.

First take a look at the pure python implementation in prime_sieve/list.py. Then see the numpy implementation in prime_sieve/array.py. Sieve operations that are independent of the actual computation of primes, such as looking up the nth-prime number, are found in prime_sieve/base.py.

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.index_of(7)) # 4

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.

Metadata

Release files for prime-sieve 0.1.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prime-sieve 0.1.11
File Size Uploaded Provenance Actions
prime_sieve-0.1.11.tar.gz 15.6 kB Without provenance Details for prime_sieve-0.1.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for prime-sieve 0.1.11
File Interpreter ABI Platform Actions
prime_sieve-0.1.11-py2.py3-none-any.whl Python 3, Python 2 none any Details for prime_sieve-0.1.11-py2.py3-none-any.whl

Total release size: 23.7 kB

Release files / prime_sieve-0.1.11.tar.gz

Download URL prime_sieve-0.1.11.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2d856487bf1d6ba7d1ac2e18c9f7dfe82dbd3e2fb9878d353fc418449a0bba68
BLAKE2b-256 checksum
How to use checksums
6097043551c38bb3026b77bc1ee61472a7d7e2c0db9fded087649d87fe29019f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

Release files / prime_sieve-0.1.11-py2.py3-none-any.whl

Download URL prime_sieve-0.1.11-py2.py3-none-any.whl
Size 8.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f6c0deab2049bd808f539c873ff3c6a602e7b77341cb24153e333f387d9aeabf
BLAKE2b-256 checksum
How to use checksums
b97376eff9ea1aef7ab381cc68135bcd856174dbf364ac634512f7bae134ffe7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

Security for prime-sieve 0.1.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page