Skip to main content

A powerful Python package for prime number operations

Project description

🔢 abhiprime

PyPI version Python versions License: MIT Tests codecov

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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.ntheory and primesieve

⭐ Star this repo if you find it useful!

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

abhiprime-7.0.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

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

abhiprime-7.0.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file abhiprime-7.0.0.tar.gz.

File metadata

  • Download URL: abhiprime-7.0.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for abhiprime-7.0.0.tar.gz
Algorithm Hash digest
SHA256 cf8280138e9081d92283ba7fc2a6096c4bedb1ede9fd5d56c05cbdc1c2ec3647
MD5 85df6289ccfdb4316c88cd9e6bb90ff2
BLAKE2b-256 ca53a610faba5a22707052929666c23bd11c5c0069867adbd46f2f8b4ead7868

See more details on using hashes here.

File details

Details for the file abhiprime-7.0.0-py3-none-any.whl.

File metadata

  • Download URL: abhiprime-7.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for abhiprime-7.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c27dc2cf762594b734b4bb9e61809bee828b15cb5056bc3f6fc8f8f89cb125e
MD5 d91a14a3b68f6aec7383e28039c6e01e
BLAKE2b-256 f5c2dde55f5ec5b36594ebe0eb7326dc1cc6ab7b9eca548578fbbe9b9404fad8

See more details on using hashes here.

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