2020-05-04 06:25:36 +00:00
|
|
|
// * This file is part of the uutils coreutils package.
|
|
|
|
// *
|
|
|
|
// * (c) kwantam <kwantam@gmail.com>
|
|
|
|
// *
|
|
|
|
// * For the full copyright and license information, please view the LICENSE file
|
|
|
|
// * that was distributed with this source code.
|
2015-04-30 18:23:12 +00:00
|
|
|
|
2020-05-27 06:13:08 +00:00
|
|
|
// spell-checker:ignore (ToDO) filts, minidx, minkey paridx
|
|
|
|
|
2021-04-02 21:31:22 +00:00
|
|
|
use std::iter::{Chain, Copied, Cycle};
|
2015-05-15 23:26:31 +00:00
|
|
|
use std::slice::Iter;
|
2015-04-30 18:23:12 +00:00
|
|
|
|
2015-05-15 23:26:31 +00:00
|
|
|
/// A lazy Sieve of Eratosthenes.
|
|
|
|
///
|
|
|
|
/// This is a reasonably efficient implementation based on
|
|
|
|
/// O'Neill, M. E. "[The Genuine Sieve of Eratosthenes.](http://dx.doi.org/10.1017%2FS0956796808007004)"
|
|
|
|
/// Journal of Functional Programming, Volume 19, Issue 1, 2009, pp. 95--106.
|
2022-01-30 13:59:31 +00:00
|
|
|
#[derive(Default)]
|
2015-04-30 18:23:12 +00:00
|
|
|
pub struct Sieve {
|
2015-05-15 23:26:31 +00:00
|
|
|
inner: Wheel,
|
|
|
|
filts: PrimeHeap,
|
2015-04-30 18:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Sieve {
|
|
|
|
type Item = u64;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.inner.size_hint()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<u64> {
|
2021-05-29 12:32:35 +00:00
|
|
|
for n in &mut self.inner {
|
2015-05-15 23:26:31 +00:00
|
|
|
let mut prime = true;
|
|
|
|
while let Some((next, inc)) = self.filts.peek() {
|
|
|
|
// need to keep checking the min element of the heap
|
|
|
|
// until we've found an element that's greater than n
|
|
|
|
if next > n {
|
2018-03-12 08:20:58 +00:00
|
|
|
break; // next heap element is bigger than n
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if next == n {
|
|
|
|
// n == next, and is composite.
|
|
|
|
prime = false;
|
|
|
|
}
|
|
|
|
// Increment the element in the prime heap.
|
|
|
|
self.filts.replace((next + inc, inc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if prime {
|
|
|
|
// this is a prime; add it to the heap
|
|
|
|
self.filts.insert(n);
|
2015-04-30 18:23:12 +00:00
|
|
|
return Some(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sieve {
|
2022-01-30 13:59:31 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
Self::default()
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[inline]
|
|
|
|
pub fn primes() -> PrimeSieve {
|
2022-01-30 13:59:31 +00:00
|
|
|
INIT_PRIMES.iter().copied().chain(Self::new())
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[inline]
|
|
|
|
pub fn odd_primes() -> PrimeSieve {
|
2022-01-30 13:59:31 +00:00
|
|
|
INIT_PRIMES[1..].iter().copied().chain(Self::new())
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:30:07 +00:00
|
|
|
pub type PrimeSieve = Chain<Copied<Iter<'static, u64>>, Sieve>;
|
2015-05-15 23:26:31 +00:00
|
|
|
|
|
|
|
/// An iterator that generates an infinite list of numbers that are
|
|
|
|
/// not divisible by any of 2, 3, 5, or 7.
|
|
|
|
struct Wheel {
|
|
|
|
next: u64,
|
|
|
|
increment: Cycle<Iter<'static, u64>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Wheel {
|
|
|
|
type Item = u64;
|
|
|
|
|
2015-04-30 18:23:12 +00:00
|
|
|
#[inline]
|
2015-05-15 23:26:31 +00:00
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(1, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-03-12 08:20:58 +00:00
|
|
|
fn next(&mut self) -> Option<u64> {
|
2015-05-15 23:26:31 +00:00
|
|
|
let increment = self.increment.next().unwrap(); // infinite iterator, no check necessary
|
|
|
|
let ret = self.next;
|
|
|
|
self.next = ret + increment;
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Wheel {
|
|
|
|
#[inline]
|
2022-01-30 13:59:31 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
2018-03-12 08:20:58 +00:00
|
|
|
next: 11u64,
|
|
|
|
increment: WHEEL_INCS.iter().cycle(),
|
|
|
|
}
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 13:59:31 +00:00
|
|
|
impl Default for Wheel {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 23:26:31 +00:00
|
|
|
/// The increments of a wheel of circumference 210
|
|
|
|
/// (i.e., a wheel that skips all multiples of 2, 3, 5, 7)
|
2020-02-07 04:40:45 +00:00
|
|
|
const WHEEL_INCS: &[u64] = &[
|
2018-03-12 08:20:58 +00:00
|
|
|
2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6, 6, 2, 6, 4, 2, 6, 4, 6, 8, 4, 2, 4, 2, 4, 8, 6, 4, 6, 2, 4, 6,
|
|
|
|
2, 6, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2, 10, 2, 10,
|
|
|
|
];
|
2020-02-07 04:40:45 +00:00
|
|
|
const INIT_PRIMES: &[u64] = &[2, 3, 5, 7];
|
2015-05-15 23:26:31 +00:00
|
|
|
|
|
|
|
/// A min-heap of "infinite lists" of prime multiples, where a list is
|
|
|
|
/// represented as (head, increment).
|
2022-01-30 13:59:31 +00:00
|
|
|
#[derive(Debug, Default)]
|
2015-05-15 23:26:31 +00:00
|
|
|
struct PrimeHeap {
|
|
|
|
data: Vec<(u64, u64)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PrimeHeap {
|
|
|
|
fn peek(&self) -> Option<(u64, u64)> {
|
|
|
|
if let Some(&(x, y)) = self.data.get(0) {
|
|
|
|
Some((x, y))
|
|
|
|
} else {
|
|
|
|
None
|
2015-04-30 18:23:12 +00:00
|
|
|
}
|
2015-05-15 23:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, next: u64) {
|
|
|
|
let mut idx = self.data.len();
|
|
|
|
let key = next * next;
|
|
|
|
|
|
|
|
let item = (key, next);
|
|
|
|
self.data.push(item);
|
|
|
|
loop {
|
|
|
|
// break if we've bubbled to the top
|
|
|
|
if idx == 0 {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-30 18:23:12 +00:00
|
|
|
|
2015-05-15 23:26:31 +00:00
|
|
|
let paridx = (idx - 1) / 2;
|
|
|
|
let (k, _) = self.data[paridx];
|
|
|
|
if key < k {
|
|
|
|
// bubble up, found a smaller key
|
|
|
|
self.data.swap(idx, paridx);
|
|
|
|
idx = paridx;
|
|
|
|
} else {
|
|
|
|
// otherwise, parent is smaller, so we're done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove(&mut self) -> (u64, u64) {
|
|
|
|
let ret = self.data.swap_remove(0);
|
|
|
|
|
|
|
|
let mut idx = 0;
|
|
|
|
let len = self.data.len();
|
|
|
|
let (key, _) = self.data[0];
|
|
|
|
loop {
|
2018-03-12 08:20:58 +00:00
|
|
|
let child1 = 2 * idx + 1;
|
|
|
|
let child2 = 2 * idx + 2;
|
2015-05-15 23:26:31 +00:00
|
|
|
|
|
|
|
// no more children
|
|
|
|
if child1 >= len {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find lesser child
|
|
|
|
let (c1key, _) = self.data[child1];
|
|
|
|
let (minidx, minkey) = if child2 >= len {
|
|
|
|
(child1, c1key)
|
|
|
|
} else {
|
|
|
|
let (c2key, _) = self.data[child2];
|
|
|
|
if c1key < c2key {
|
|
|
|
(child1, c1key)
|
|
|
|
} else {
|
|
|
|
(child2, c2key)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if minkey < key {
|
|
|
|
self.data.swap(minidx, idx);
|
|
|
|
idx = minidx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// smaller than both children, so done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret
|
|
|
|
}
|
2015-04-30 18:23:12 +00:00
|
|
|
|
2015-05-15 23:26:31 +00:00
|
|
|
/// More efficient than inserting and removing in two steps
|
|
|
|
/// because we save one traversal of the heap.
|
|
|
|
fn replace(&mut self, next: (u64, u64)) -> (u64, u64) {
|
|
|
|
self.data.push(next);
|
|
|
|
self.remove()
|
2015-04-30 18:23:12 +00:00
|
|
|
}
|
|
|
|
}
|