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

Coming soon...

assert primes[5:3:-1] == [11,7]

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.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

primegenerator-1.3.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: primegenerator-1.3.0.tar.gz
  • Upload date:
  • Size: 10.2 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.0.tar.gz
Algorithm Hash digest
SHA256 88205c8b145625cd2446658e74dbee0e3e0fb99cd41824701ead3a3dd72eacab
MD5 15c7da77a6665ede9d7644794fd0fafd
BLAKE2b-256 b1356a8caca7952f770137a0fd998a378064065ea6cee76e4a6ed41d5f47c168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primegenerator-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eff98a491dac3c11efe6ef097ca5aef0c279eb76762707a6e944e194d9e437b3
MD5 d924d8f171dc021241b3a0a5747ed7b3
BLAKE2b-256 1fed1133dc9fc0a4c53fe1536671e6a742803c64c3ebe1e273969f43536af6a4

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