[][src]Macro reikna::segmented_sieve

macro_rules! segmented_sieve {
    ($max:ident, $candidate:ident, $callback:expr) => { ... };
}

Macro representing the body of a segmented Sieve of Eratosthenes.

This macro is useful for preforming operations on ranges of prime numbers that would be too large to store in memory (such as the prime_sieve() family of functions.

The syntax for this macro is

segmented_sieve(max (identifier), candidate (identifier),
                callback (expression));

Where max is an identifier where the max value for the sieve can be found, candidate is an identifier that exposes each prime found by the sieve, and expression is a callback expression that is ran every time a prime is found.

Note -- this routine will never include the number 2! It is a special case and must be dealt with separately.

Note -- this macro assumes that several members of prime are also in scope! The following use statement should work:

use reikna::prime::{Bitset, S_SIEVE_SIZE, prime_sieve};

Panics

Panics if prime_sieve() panics. See the documentation of prime_sieve() for more information.

Examples

Find the sum of the primes in [0..1,000,000].

#[macro_use] extern crate reikna;
use reikna::prime::{Bitset, S_SIEVE_SIZE, prime_sieve};
fn main() {
    let max = 1_000_000;
    let mut sum = 2;

    segmented_sieve!(max, candidate, sum += candidate);

    println!("Sum of primes in [0, {}] -- {}", max, sum);
}