Skip to main content

Introduction

Fast PrimeNumbers generator which caches previous results across instances.
You usually don't need as many primes as the worst-case - eg for the included lcm (lowest common multiplier) function.

Also provides a very fast primesieve if you wish to pre-calculate a large number of primes.

Getting Started

Installation: pip install primegenerator

Main usage:

from primegenerator import PrimeNumbers
primes = PrimeNumbers()
for prime in primes:
    ... #do something
    if ... #beware this is an infinite iterator!
        break

Test if number is prime:

from primegenerator import PrimeNumbers
assert 5 in PrimeNumbers()
assert 9 not in PrimeNumbers()

Get a specific prime:

from primegenerator import PrimeNumbers
primes = PrimeNumbers(startat1=True)
assert primes[3] == 5
assert primes[3:5] == [5,7]

Preseed - if you know you need all primes up to n:

from primegenerator import PrimeNumbers
n = 100 #somebignumber
primes = PrimeNumbers.preseed(n)
for prime in primes:
    ... #do something
    if ... #beware this is still an infinite iterator and will keep going past the seed point!
        break

Lowest common multiplier:

from primegenerator import lcm
numbers = [2,3,4]
assert lcm(numbers) == 12
assert lcm(3,4,5) == 60

Sieve:

from primegenerator import primesieve
listofprimes = primesieve(maxprime)

Build and Test

Tests are written for pytest in /tests/test_*.py
pip -r tests/requirements.txt for additional imports required for some tests.
Tests are available on the ADO repository (see link below)

/dev contains some useful stuff for debugging, profiling and further development and is only available on the ADO repository (see link below)

Contribute

Code repository (ADO): https://dev.azure.com/MusicalNinjas/MikesMath/_git/primes
Homepage: https://dev.azure.com/MusicalNinjas/MikesMath

What's New?

v1.3.1

  • Slicing supports negative step

v1.3.0

  • Get a specific prime with primes[n] or primes[n:m]

v1.2.1

  • test_wrapper improvements

v1.2.0

  • n in PrimeNumbers() will test if n is prime

v1.1.0

  • lcm will take multiple arguments and find lowest common multiplier. (Previously required a single list or tuple)

v1.0.0

  • PrimeNumbers() infinite iterator supporting startat1, .preseed()
  • lcm to find lowest common multiplier
  • sieve to return a list of primes

Coming next...

Able to launch a parallel process to pre-calculate the next prime needed on a separate CPU core

Metadata

Release files for primegenerator 1.3.1

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

Source distribution (sdist)

Source distribution for primegenerator 1.3.1
File Size Uploaded Provenance
primegenerator-1.3.1.tar.gz 10.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for primegenerator 1.3.1
File Interpreter ABI Platform
primegenerator-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / primegenerator-1.3.1.tar.gz

Download URL primegenerator-1.3.1.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58f5482a33314bd3916eb6c8454c9b0e7198cca302130dc3d421af0d059c37d4
BLAKE2b-256 checksum
How to use checksums
5f8eb68b3973881e994385dd0d146248774c3293edf3ce8405add4f3ec964ae3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / primegenerator-1.3.1-py3-none-any.whl

Download URL primegenerator-1.3.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c6e7f0eea42c4c8bba6c1534901ab6fcb5144b14de97ca791c1794df15e9d2e
BLAKE2b-256 checksum
How to use checksums
838074c194aba378f2f879543047274eb689c0dbae882ddb0f8ded21befcf6e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Security for primegenerator 1.3.1

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