Skip to main content

primes

This module contains several useful functions to work with prime numbers. For example, extracting all the prime factors (with multiplicity) of a positive integer reasonably fast. Following the list of all functions and their running time.

Getting started

Download the file primes.py and place it in the same directory where your python is installed. Or, simply run the command

>>>pip install primes

to install the package. After either of the above two methods you can call it by

>>>import primes

and then execute the available methods.

Available methods

You may run primes.about() afer importing the package. The following is a list of all included methods.

primes.check(n) returns True if n is a prime number.
primes.factor(n) returns the lowest prime factor of n.
primes.facors(n) returns all the prime factors of n with multiplicity.
primes.first(n) returns first n many primes.
primes.upto(n) returns all the primes less than or equal to n.
primes.between(m,n) returns all the primes between m and n.
primes.phi(n) returns the Euler's phi(n) i.e., the number of integers less than n which have no common factor with n.

Demonstration

This program is tested on my personal laptop with the following configurations.

Processor: Intel(R) Core(TM) i3-4030U CPU @ 1.90Ghz
Installed memory(RAM): 6.00GB
System type: 64 bit Operating System, x64-based processor
Operating system: Windows 10

Small numbers

All the following commands returnd results in less than 1 sec.

>>> primes.check(56156149)
False
>>> primes.check(79012338765433)
True
>>> primes.factor(7568945625)
3
>>> primes.factor(5141)
53
>>> primes.factors(252)
[2, 2, 3, 3, 7]
>>> primes.factors(44410608)
[2, 2, 2, 2, 3, 3, 11, 23, 23, 53]
>>> primes.first(7)
[2, 3, 5, 7, 11, 13, 17]
>>> primes.first(37)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157]
>>> primes.first(5000)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
. . . . 
. . . .
 48179, 48187, 48193, 48197, 48221, 48239, 48247, 48259, 48271, 48281, 48299, 48311, 48313, 48337, 48341, 48353, 48371, 48383, 48397, 48407, 48409, 48413, 48437, 48449, 48463, 48473, 48479, 48481, 48487, 48491, 48497, 48523, 48527, 48533, 48539, 48541, 48563, 48571, 48589, 48593, 48611]

Outcomes from the last command is truncated.

>>> primes.upto(16)
[2, 3, 5, 7, 11, 13]
>>> primes.upto(50000)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179
. . .
. . .
49789, 49801, 49807, 49811, 49823, 49831, 49843, 49853, 49871, 49877, 49891, 49919,
49921, 49927, 49937, 49939, 49943, 49957, 49991, 49993, 49999]
>>> primes.between(100,200)
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
>>> primes.between(100000,500000)
[100003, 100019, 100043, 100049, 100057, 100069, 100103, 100109, 100129, 100151, 100153,
100169, 100183, 100189, 100193, 100207, 100213, 100237, 100267, 100271, 100279, 100291

499661, 499663, 499669, 499673, 499679, 499687, 499691, 499693, 499711, 499717, 499729, 499739, 499747, 499781, 499787, 499801, 499819, 499853, 499879, 499883, 499897, 499903, 499927, 499943, 499957, 499969, 499973, 499979]
>>> primes.phi(128)
64
>>> primes.phi(561534567567457)
483618287856960

A little bigger numbers

All the following commands returned results in less than 5 secs.

>>> primes.factors(2910046587320501324077792713140104371205630933992706145011)
[239, 701, 709, 1997, 1997, 3889, 5171, 5171, 6983, 10009, 4940867, 45845791, 3731292319]
>>> primes.first(10000)[9999]
104729

The last command returns the 10000th prime.

Suggestions

Feel free to drop your suggestion at the following email address

Author: Indrajit Jana
Email: ijana at temple dot edu

Metadata

Release files for primePy 1.0

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

Source distribution (sdist)

Source distribution for primePy 1.0
File Size Uploaded Provenance Actions
primePy-1.0.tar.gz 3.1 kB Without provenance Details for primePy-1.0.tar.gz

Built distribution (wheel)

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

Total release size: 5.9 kB

Release files / primePy-1.0.tar.gz

Download URL primePy-1.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
53403569077e4d7f7ba7ac53096e4bc05ddf86f996695b7282d981eb0da765d1
BLAKE2b-256 checksum
How to use checksums
b5901214274d75ca5496a310c9ac0f6d88708f570b4f47eb0e4989d7a011331e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / primePy-1.0-py3-none-any.whl

Download URL primePy-1.0-py3-none-any.whl
Size 2.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80a9ff144dc5b085acc795dd1f44da7d9c55272f5e9ebaaa76c98cef2a111450
BLAKE2b-256 checksum
How to use checksums
29204b9acaae72250f570f42353af56fe5927c3975b8fe2f68fcb390f9ce8ddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for primePy 1.0

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.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

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