|
| 1 | +/* |
| 2 | +Linear Sieve algorithm: |
| 3 | +Time complexity is indeed O(n) with O(n) memory, but the sieve generally |
| 4 | +runs slower than a well implemented sieve of Eratosthenes. Some use cases are: |
| 5 | +- factorizing any number k in the sieve in O(log(k)) |
| 6 | +- calculating arbitrary multiplicative functions on sieve numbers |
| 7 | + without increasing the time complexity |
| 8 | +- As a by product, all prime numbers less than `max_number` are stored |
| 9 | + in `primes` vector. |
| 10 | + */ |
| 11 | +pub struct LinearSieve { |
| 12 | + max_number: usize, |
| 13 | + primes: Vec<usize>, |
| 14 | + minimum_prime_factor: Vec<usize>, |
| 15 | +} |
| 16 | + |
| 17 | +impl LinearSieve { |
| 18 | + pub const fn new() -> Self { |
| 19 | + LinearSieve { |
| 20 | + max_number: 0, |
| 21 | + primes: vec![], |
| 22 | + minimum_prime_factor: vec![], |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn prepare(&mut self, max_number: usize) -> Result<(), &'static str> { |
| 27 | + if max_number <= 1 { |
| 28 | + return Err("Sieve size should be more than 1"); |
| 29 | + } |
| 30 | + if self.max_number > 0 { |
| 31 | + return Err("Sieve already initialized"); |
| 32 | + } |
| 33 | + self.max_number = max_number; |
| 34 | + self.minimum_prime_factor.resize(max_number + 1, 0); |
| 35 | + for i in 2..=max_number { |
| 36 | + if self.minimum_prime_factor[i] == 0 { |
| 37 | + self.minimum_prime_factor[i] = i; |
| 38 | + self.primes.push(i); |
| 39 | + /* |
| 40 | + if needed, a multiplicative function can be |
| 41 | + calculated for this prime number here: |
| 42 | + function[i] = base_case(i); |
| 43 | + */ |
| 44 | + } |
| 45 | + for p in self.primes.iter() { |
| 46 | + let mlt = (*p) * i; |
| 47 | + if *p > i || mlt > max_number { |
| 48 | + break; |
| 49 | + } |
| 50 | + self.minimum_prime_factor[mlt] = *p; |
| 51 | + /* |
| 52 | + multiplicative function for mlt can be calculated here: |
| 53 | + if i % p: |
| 54 | + function[mlt] = add_to_prime_exponent(function[i], i, p); |
| 55 | + else: |
| 56 | + function[mlt] = function[i] * function[p] |
| 57 | + */ |
| 58 | + } |
| 59 | + } |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn factorize(&self, mut number: usize) -> Result<Vec<usize>, &'static str> { |
| 64 | + if number > self.max_number { |
| 65 | + return Err("Number is too big, its minimum_prime_factor was not calculated"); |
| 66 | + } |
| 67 | + if number == 0 { |
| 68 | + return Err("Number is zero"); |
| 69 | + } |
| 70 | + let mut result: Vec<usize> = Vec::new(); |
| 71 | + while number > 1 { |
| 72 | + result.push(self.minimum_prime_factor[number]); |
| 73 | + number /= self.minimum_prime_factor[number]; |
| 74 | + } |
| 75 | + Ok(result) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::LinearSieve; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn small_primes_list() { |
| 85 | + let mut ls = LinearSieve::new(); |
| 86 | + ls.prepare(25).unwrap(); |
| 87 | + assert_eq!(ls.primes, vec![2, 3, 5, 7, 11, 13, 17, 19, 23]); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn divisible_by_mpf() { |
| 92 | + let mut ls = LinearSieve::new(); |
| 93 | + ls.prepare(1000).unwrap(); |
| 94 | + for i in 2..=1000 { |
| 95 | + let div = i / ls.minimum_prime_factor[i]; |
| 96 | + assert_eq!(i % ls.minimum_prime_factor[i], 0); |
| 97 | + if div == 1 { |
| 98 | + // Number must be prime |
| 99 | + assert!(ls.primes.binary_search(&i).is_ok()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn check_factorization() { |
| 106 | + let mut ls = LinearSieve::new(); |
| 107 | + ls.prepare(1000).unwrap(); |
| 108 | + for i in 1..=1000 { |
| 109 | + let factorization = ls.factorize(i).unwrap(); |
| 110 | + let mut product = 1usize; |
| 111 | + for (idx, p) in factorization.iter().enumerate() { |
| 112 | + assert!(ls.primes.binary_search(&p).is_ok()); |
| 113 | + product *= *p; |
| 114 | + if idx > 0 { |
| 115 | + assert!(*p >= factorization[idx - 1]); |
| 116 | + } |
| 117 | + } |
| 118 | + assert_eq!(product, i); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn check_number_of_primes() { |
| 124 | + let mut ls = LinearSieve::new(); |
| 125 | + ls.prepare(100_000).unwrap(); |
| 126 | + assert_eq!(ls.primes.len(), 9592); |
| 127 | + } |
| 128 | +} |
0 commit comments