🔢 abhiprime
A powerful, efficient, and comprehensive Python package for prime number operations.
✨ Features
- 🔍 Primality Testing: Trial division + Miller-Rabin for all number sizes
- ⚡ High Performance: Sieve of Eratosthenes (standard & segmented)
- 🎯 Advanced Algorithms: Lucas-Lehmer, Baillie-PSW, prime counting
- 📊 Mathematical Functions: Twin primes, Goldbach partitions, prime gaps
- 🚀 CLI Tool: Command-line interface for quick operations
- 💾 Smart Caching: LRU cache for repeated queries
- 📝 Type Hints: Full type annotation support
- 🧪 Well Tested: Comprehensive test suite with >95% coverage
📦 Installation
pip install abhiprime
Upgrade from v1:
pip install --upgrade abhiprime
🚀 Quick Start
Basic Usage
import abhiprime as ap
# Test primality
ap.test_prime(17) # True
ap.test_prime(18) # False
# Find nearby primes
ap.prev_prime(20) # 19
ap.next_prime(20) # 23
# Get primes in range
ap.prime_upto(50) # [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
ap.range_prime(10, 30) # [11, 13, 17, 19, 23, 29]
# Prime factorization
ap.prime_factors(60) # [2, 2, 3, 5]
Advanced Features
from abhiprime import (
prime_count, nth_prime, twin_primes,
goldbach_partitions, segmented_sieve, PrimeCache
)
# Prime counting function π(n)
prime_count(100) # 25
prime_count(1000) # 168
# Find the nth prime
nth_prime(100) # 541
nth_prime(10000) # 104729
# Special prime pairs
twin_primes(50) # [(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43)]
cousin_primes(50) # [(3, 7), (7, 11), (13, 17), (19, 23), (37, 41), (43, 47)]
sexy_primes(50) # [(5, 11), (7, 13), (11, 17), (13, 19), (17, 23), ...]
# Goldbach partitions
goldbach_partitions(100) # [(3, 97), (11, 89), (17, 83), (29, 71), (41, 59), (47, 53)]
# Memory-efficient segmented sieve for large ranges
segmented_sieve(10**12, 10**12 + 1000)
# Caching for repeated operations
cache = PrimeCache(maxsize=10000)
cache.is_prime(104729) # Cached result
cache.stats() # {'prime_cache_size': 1, 'sieve_cache_size': 0}
Large Number Support
from abhiprime import miller_rabin, baillie_psw, lucas_lehmer
# Probabilistic test for large numbers (cryptography-grade)
miller_rabin(2**61 - 1, k=20) # True (Mersenne prime)
# Baillie-PSW (no known counterexamples)
baillie_psw(3825123056546413051)
# Lucas-Lehmer for Mersenne primes
lucas_lehmer(127) # True (2^127 - 1 is prime)
🖥️ Command Line Interface
# Test if a number is prime
abhiprime test 17
# Output: 17 is prime
# Get primes up to n
abhiprime upto 50
# Output: Found 15 primes
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
# Prime factorization
abhiprime factors 60
# Output: Prime factors of 60: [2, 2, 3, 5]
# Count primes
abhiprime count 1000
# Output: π(1000) = 168
# Find nth prime
abhiprime nth 100
# Output: Prime #100 = 541
# Twin primes
abhiprime twins 100
# Output: Found 8 pairs
# (3, 5)
# (5, 7)
# ...
# Goldbach partitions
abhiprime goldbach 100
# Output: Goldbach partitions of 100:
# 3 + 97 = 100
# 11 + 89 = 100
# ...
# JSON output
abhiprime --format json upto 20
# Output: {"upper_bound": 20, "count": 8, "primes": [2, 3, 5, 7, 11, 13, 17, 19]}
📊 Performance Comparison
| Operation | abhiprime v2 | abhiprime v1 | sympy |
|---|---|---|---|
prime_upto(10^6) |
0.05s | 2.3s | 0.08s |
prime_upto(10^8) |
4.2s | N/A | 5.1s |
test_prime(10^18) |
0.001s | 0.8s | 0.002s |
nth_prime(10^6) |
0.3s | N/A | 0.4s |
Benchmarks on Intel i7, Python 3.11
🔧 API Reference
Core Functions
| Function | Description | Time Complexity |
|---|---|---|
test_prime(n) |
Primality test | O(√n) small, O(k·log³n) large |
prev_prime(n) |
Largest prime < n | O(√n · gap) |
next_prime(n) |
Smallest prime > n | O(√n · gap) |
prime_upto(n) |
All primes ≤ n | O(n log log n) |
range_prime(a, b) |
Primes in [a, b] | O(n log log n) |
prime_factors(n) |
Prime factorization | O(√n) |
Advanced Functions
| Function | Description |
|---|---|
miller_rabin(n, k=10) |
Probabilistic primality test |
lucas_lehmer(p) |
Mersenne prime test |
baillie_psw(n) |
Deterministic probable prime test |
prime_count(n) |
Count primes ≤ n |
nth_prime(n) |
Find the nth prime |
twin_primes(n) |
Find twin prime pairs |
goldbach_partitions(n) |
Even number as sum of two primes |
segmented_sieve(low, high) |
Memory-efficient large range sieve |
prime_generator() |
Infinite prime generator |
🧪 Running Tests
# Clone the repository
git clone https://github.com/abhi1628/prime-number-python-package.git
cd prime-number-python-package
# Install dependencies
pip install -e ".[dev]"
# Run tests
pytest tests/ -v --cov=abhiprime
# Run benchmarks
python benchmarks/benchmark.py
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Sieve of Eratosthenes implementation optimized with
bytearray - Miller-Rabin implementation based on deterministic variants
- Inspired by
sympy.ntheoryandprimesieve
⭐ Star this repo if you find it useful!
Metadata
Release files for abhiprime 7.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| abhiprime-7.0.0.tar.gz | 15.9 kB | Without provenance | Details for abhiprime-7.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| abhiprime-7.0.0-py3-none-any.whl | Python 3 | none | any | Details for abhiprime-7.0.0-py3-none-any.whl |
Total release size: 30.0 kB
Release files / abhiprime-7.0.0.tar.gz
| Download URL | abhiprime-7.0.0.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf8280138e9081d92283ba7fc2a6096c4bedb1ede9fd5d56c05cbdc1c2ec3647
|
|
BLAKE2b-256 checksum How to use checksums |
ca53a610faba5a22707052929666c23bd11c5c0069867adbd46f2f8b4ead7868
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|
Release files / abhiprime-7.0.0-py3-none-any.whl
| Download URL | abhiprime-7.0.0-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c27dc2cf762594b734b4bb9e61809bee828b15cb5056bc3f6fc8f8f89cb125e
|
|
BLAKE2b-256 checksum How to use checksums |
f5c2dde55f5ec5b36594ebe0eb7326dc1cc6ab7b9eca548578fbbe9b9404fad8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.4
|
Security for abhiprime 7.0.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.