pub struct Sieve { /* private fields */ }Expand description
エラトステネスの篩で素数と最小素因数を前計算します。
0..=limit の範囲で素数判定と素因数分解を行うため、各整数の最小素因数を保持します。
is_prime は O(1)、prime_factorize は素因数の重複度(指数の合計)に比例する
O(log n) 時間で計算できます。素数列挙は primes() から行えます。
構築の計算量は O(N log log N)、使用メモリは O(N) です。
§Examples
use atcoder::math::sieve::Sieve;
let sieve = Sieve::new(100);
assert!(sieve.is_prime(97));
assert!(!sieve.is_prime(1));
assert_eq!(
sieve.prime_factorize(84),
vec![(2, 2), (3, 1), (7, 1)],
);Implementations§
Source§impl Sieve
impl Sieve
Sourcepub fn new(limit: usize) -> Self
pub fn new(limit: usize) -> Self
0..=limit の各整数について最小素因数を前計算します。判定・分解可能な値はこの範囲内です。
計算量は O(limit log log limit)、使用メモリは O(limit) です。
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Sieve
impl RefUnwindSafe for Sieve
impl Send for Sieve
impl Sync for Sieve
impl Unpin for Sieve
impl UnsafeUnpin for Sieve
impl UnwindSafe for Sieve
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more