Skip to main content

Fast primes generator and related functions

Project description

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

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

primegenerator-1.3.1.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

primegenerator-1.3.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file primegenerator-1.3.1.tar.gz.

File metadata

  • Download URL: primegenerator-1.3.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for primegenerator-1.3.1.tar.gz
Algorithm Hash digest
SHA256 58f5482a33314bd3916eb6c8454c9b0e7198cca302130dc3d421af0d059c37d4
MD5 93713af4b2748743ff7bed558ca79608
BLAKE2b-256 5f8eb68b3973881e994385dd0d146248774c3293edf3ce8405add4f3ec964ae3

See more details on using hashes here.

File details

Details for the file primegenerator-1.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for primegenerator-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c6e7f0eea42c4c8bba6c1534901ab6fcb5144b14de97ca791c1794df15e9d2e
MD5 b9e8d9c794213bbe57b05c164a31d1fc
BLAKE2b-256 838074c194aba378f2f879543047274eb689c0dbae882ddb0f8ded21befcf6e8

See more details on using hashes here.

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