Git Product home page Git Product logo

prime_sieve's Introduction

Prime Sieve

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.

prime_sieve's People

Contributors

haksell avatar jamesmurphy-mc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.