sieve-of-eratosthenes

Crates.iosieve-of-eratosthenes
lib.rssieve-of-eratosthenes
version0.1.2
created_at2026-01-04 15:46:49.945023+00
updated_at2026-01-04 18:03:53.90965+00
descriptionA standard implementation of the Sieve of Eratosthenes
homepage
repositoryhttps://github.com/michaeltinsley/rust-fundamentals
max_upload_size
id2022089
size22,164
Michael Tinsley (michaeltinsley)

documentation

README

Sieve of Eratosthenes

Crates.io Documentation License

A high-performance, memory-efficient, and portable implementation of the Sieve of Eratosthenes algorithm in Rust.

This library is designed for speed and efficiency, using a bit-packed representation (SWAR) to minimize memory usage and maximize CPU cache locality.

Features

  • 🚀 Fast: Calculates primes up to 1,000,000 in ~3ms on modern hardware (M2 Air).
  • 💾 Memory Efficient: Uses Vec<usize> bit-packing (~1 bit per number).
  • 🔧 Portable: Automatically adapts to native word size (64-bit on x64/ARM64, 32-bit on ARMv7/x86).
  • 📦 Zero Dependencies: Pure Rust with no external crates.
  • 🛡️ Safe: 100% safe Rust (no unsafe blocks required for public API).

Installation

Add this to your Cargo.toml:

[dependencies]
sieve-of-eratosthenes = "0.1.0"

Usage

use sieve_of_eratosthenes::SieveOfEratosthenes;

fn main() {
    // Find all primes up to 100
    let sieve = SieveOfEratosthenes::new(100);

    // Check individual numbers (O(1) lookup)
    assert!(sieve.is_prime(2));
    assert!(sieve.is_prime(97));
    assert!(!sieve.is_prime(100)); // 100 is composite

    // Iterate over primes
    for prime in sieve.iter() {
        println!("{}", prime);
    }
}
Commit count: 0

cargo fmt