Skip to main content

Sieve

Struct Sieve 

Source
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

Source

pub fn new(limit: usize) -> Self

0..=limit の各整数について最小素因数を前計算します。判定・分解可能な値はこの範囲内です。

計算量は O(limit log log limit)、使用メモリは O(limit) です。

Source

pub fn limit(&self) -> usize

前計算した範囲の上端 limit を返します(有効範囲は 0..=limit())。

Source

pub fn is_prime(&self, n: usize) -> bool

前計算範囲内の n が素数かどうかを返します。0 と 1 は false です。

計算量は O(1) です。

§Panics

n > limit の場合は panic します。

Source

pub fn primes(&self) -> impl Iterator<Item = usize> + '_

0..=limit() に含まれる素数を昇順に列挙します。

全要素を列挙する計算量は O(limit()) です。イテレータは self を借用するため、 Sieve より長く保持することはできません。

Source

pub fn prime_factorize(&self, n: usize) -> Vec<(usize, usize)>

前計算範囲内の n を素因数分解し、(素因数, 指数) の組を素因数の昇順で返します。

n = 1 の場合は空の Vec を返します。n = 0 は素因数分解できません。 計算量は素因数の重複度を含めて O(log n) です。

§Panics
  • n = 0 の場合
  • n > limit の場合

Trait Implementations§

Source§

impl Clone for Sieve

Source§

fn clone(&self) -> Sieve

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sieve

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.