Skip to main content

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.

Metadata

Release files for nprime 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 nprime 1.3.1
File Size Uploaded
nprime-1.3.1.tar.gz 149.0 kB Details

Built distribution (wheel)

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

Total release size: 169.7 kB

Release files / nprime-1.3.1.tar.gz

Download URL nprime-1.3.1.tar.gz
Size 149.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2375c4e94ede934e4a8ce06f8e1f57024d925c7559d3e89e902247c7275464c5
BLAKE2b-256 checksum
How to use checksums
a08e34a0f95a21dbe242066615d7233aae82e23ad8011cbbd0999ec008ecbf61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.

Transparency log

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

Download URL nprime-1.3.1-py3-none-any.whl
Size 20.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
55a65ce6c832e8893d4f6efb73f04d81a20f63af53e1881e89c30e687ff7039c
BLAKE2b-256 checksum
How to use checksums
16093738d8dff1e6943bddfb23bd8dbb1aa13bf38cd4bd2f22d842356bf22af1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.2.1

2 release files

1.1.1

1 release file

1.0.0

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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