Skip to main content

Ultra-fast prime lookup using memory-mapped odd-only sieve

Project description

lookprime

lookprime is a high-performance prime number library for Python built around a memory-mapped, odd-only, bitset sieve.

It is designed for: - extremely fast primality tests - fast prime counting and enumeration - repeated queries across processes - large limits with minimal RAM overhead - CLI benchmarking and inspection

The core idea:
👉 build a prime sieve once, store it on disk, then reuse it via mmap with near-zero startup cost.


Features

Core performance

  • Odd-only sieve (½ the memory of full sieve)
  • Bitset representation (1 bit per odd number)
  • Memory-mapped cache (mmap) for zero-copy sharing
  • int.from_bytes(...).bit_count() for fast counting
  • Auto-growing cache when needed
  • Safe for multi-process use (read-only)

Python API

  • isprime(n)
  • isprime_many(iterable)
  • primerange(a, b)
  • randprime(a, b)
  • primepi(n)
  • prime(n) (nth prime, 1-indexed)
  • prevprime(n)
  • nextprime(n)
  • primes(n) (first n primes)
  • primes_up_to(n)
  • factorint(n)
  • cache_info()
  • clear_cache()

CLI

  • lookprime info
  • lookprime build
  • lookprime benchmark
  • lookprime isprime <n>
  • lookprime factor <n>
  • lookprime clear-cache

Installation

pip install lookprime

Python ≥ 3.9 required.


Quick Start (Python)

import lookprime

lookprime.isprime(97)                 # True
lookprime.primepi(1_000_000)          # 78498
lookprime.primes_up_to(50)            # [2, 3, 5, 7, 11, ...]
lookprime.factorint(123456)           # {2: 6, 3: 1, 643: 1}

Bulk checks:

lookprime.isprime_many([2, 3, 4, 5, 10**9 + 7])

How the Cache Works

  • Sieve files are stored in a platform-appropriate cache directory:

    • Linux: ~/.cache/lookprime
    • macOS: ~/Library/Caches/lookprime
    • Windows: %LOCALAPPDATA%\lookprime\Cache
  • Each sieve is stored as:

    lookprime_mask_<limit>.lpm
    
  • Files are memory-mapped (mmap) and shared across processes

  • Rebuilding happens only if needed

To inspect cache state:

lookprime.cache_info()

To delete all cached sieves:

lookprime.clear_cache()

or from CLI:

lookprime clear-cache

CLI Usage

Show system and cache info

lookprime info

Build a sieve explicitly

lookprime build --limit 100000000

Benchmark lookup speed

lookprime benchmark

Options:

lookprime benchmark \
  --limit 100000000 \
  --duration 1.0 \
  --iterations 25 \
  --chunk 65536

Environment override:

export LOOKPRIME_CHUNK=65536
lookprime benchmark

Primality from CLI

lookprime isprime 104729

Factor integers from CLI

lookprime factor 123456

Performance Notes

  • isprime(n) is O(1) after cache load
  • primepi(n) uses bitset population count
  • prime(n) and primes(n) use fast bit scanning
  • Cache memory is shared between processes
  • No Python objects are created per lookup

Threading & Multiprocessing

  • The sieve is read-only
  • Safe to open in multiple processes
  • Each process memory-maps its own view
  • No locks required

License

MIT License.

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

lookprime-0.1.4.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

lookprime-0.1.4-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file lookprime-0.1.4.tar.gz.

File metadata

  • Download URL: lookprime-0.1.4.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for lookprime-0.1.4.tar.gz
Algorithm Hash digest
SHA256 ae2942e7c9a40b5a7e2b4d0c00c1d7f0d58ffbbb6be973e77f417cfa5706a576
MD5 a378ae773c4f68274ed812db9b34eae8
BLAKE2b-256 1d42c6945b05a2bf2dbc50448f20e721b1c88ae93e75e42004c2e0d0993d1896

See more details on using hashes here.

File details

Details for the file lookprime-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: lookprime-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for lookprime-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 aa8ded693e2cddf6512cd91392a7c63e8cc5df45ab53c6251ef9e371dc247bbf
MD5 d4c88da76a3f801e4916651898cf785e
BLAKE2b-256 b038a9bf95846e2be801a8b1df226fd057ba69249501ae0f166c81b76f94edd8

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