Skip to main content

Python library for primes algorithms

Project description

nprime

Generic badge PyPI downloads PyPI version Build Status codecov

Installation

To install the package use pip:

pip install nprime

Introduction

Some algorithm on prime numbers. You can find all the functions in the file nprime/pryprime.py

Algorithm developed :

  • Eratosthenes sieve based
  • Fermat's test (based on Fermat's theorem)
  • Prime generating functions
  • Miller Rabin predictive algorithm

Specifications

  • Language: Python 3.9+ (supports Python 3.9, 3.10, 3.11, 3.12, 3.13)
  • Package:
    • Basic python packages were preferred
    • Matplotlib >=3.5.0 - graph and math

Integration and pipeline

For the tests coverage, there's codecov which is run during the Travis CI pipeline.

Math

Here are a bit of information to help understand some of the algorithms

Congruence

"" means congruent, a ≡ b (mod m) implies that m / (a-b), ∃ k ∈ Z that verifies a = kn + b

which implies:

a ≡ 0 (mod n) <-> a = kn <-> "a" is divisible by "n" 

Strong Pseudoprime

A strong pseudoprime to a base a is an odd composite number n with n-1 = d·2^s (for d odd) for which either a^d = 1(mod n) or a^(d·2^r) = -1(mod n) for some r = 0, 1, ..., s-1

Erathostene's Sieve

How to use

Implementation of the sieve of erathostenes that discover the primes and their composite up to a limit. It returns a dictionary:

  • the key are the primes up to n
  • the value is the list of composites of these primes up to n
from nprime import sieve_eratosthenes

# With as a parameter the upper limit
sieve_eratosthenes(10)
>> {2: [4, 6, 8, 10], 3: [9], 5: [], 7: []}

The previous behaviour can be called using the trial_division which uses the Trial Division algorithm

Theory

This sieve mark as composite the multiple of each primes. It is an efficient way to find primes. For n ∈ N with n > 2 and for ∀ a ∈[2, ..., √n] then n/a ∉ N is true.

Erathostene example

Fermat's Theorem

How to use

A Probabilistic algorithm taking t randoms numbers a and testing the Fermat's theorem on number n > 1 Prime probability is right is 1 - 1/(2^t) Returns a boolean: True if n passes the tests.

from nprime import fermat

# With n the number you want to test
fermat(n)

Theory

If n is prime then ∀ a ∈[1, ..., n-1]

    a^(n-1) ≡ 1 (mod n) ⇔ a^(n-1) = kn + 1

Miller rabin

How to use

A probabilistic algorithm which determines whether a given number (n > 1) is prime or not. The miller_rabin tests is repeated t times to get more accurate results. Returns a boolean: True if n passes the tests.

from nprime import miller_rabin

# With n the number you want to test
miller_rabin(n)

Theory

For n ∈ N and n > 2,
Take a random a ∈ {1,...,n−1}
Find d and s such as with n - 1 = 2^s * d (with d odd)
if (a^d)^2^r ≡ 1 mod n for all r in 0 to s-1
Then n is prime.

The test output is false of 1/4 of the "a values" possible in n, so the test is repeated t times.

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

nprime-1.3.1.tar.gz (149.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nprime-1.3.1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nprime-1.3.1.tar.gz
  • Upload date:
  • Size: 149.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nprime-1.3.1.tar.gz
Algorithm Hash digest
SHA256 2375c4e94ede934e4a8ce06f8e1f57024d925c7559d3e89e902247c7275464c5
MD5 5306e2b8a9f1b0f27b613483e91b0202
BLAKE2b-256 a08e34a0f95a21dbe242066615d7233aae82e23ad8011cbbd0999ec008ecbf61

See more details on using hashes here.

Provenance

The following attestation bundles were made for nprime-1.3.1.tar.gz:

Publisher: ci.yml on sylhare/nprime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: nprime-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nprime-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 55a65ce6c832e8893d4f6efb73f04d81a20f63af53e1881e89c30e687ff7039c
MD5 de053f9b9a863cd4966b4161e54f9a9e
BLAKE2b-256 16093738d8dff1e6943bddfb23bd8dbb1aa13bf38cd4bd2f22d842356bf22af1

See more details on using hashes here.

Provenance

The following attestation bundles were made for nprime-1.3.1-py3-none-any.whl:

Publisher: ci.yml on sylhare/nprime

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page