Struct nekolib::math::SieveN2Plus1

source ·
pub struct SieveN2Plus1 { /* private fields */ }
Expand description

$n^2+1$ 型素数の篩。

Idea

$n^2+1$ が素因数 $p < n$ を持つとき、以下が成り立つ。 $$ n^2+1 \equiv 0 \pmod{p}. $$ このとき、各 $k$ に対して以下が成り立つ。

  • $(n+kp)^2+1 \equiv 0 \pmod{p}$,
  • $(kp-n)^2+1 \equiv 0 \pmod{p}$.

References

See also

Implementations§

source§

impl SieveN2Plus1

source

pub fn new(n: usize) -> Self

初期化する。

source

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

$n^2+1$ の形の素数を返す。

Examples
use nekolib::math::SieveN2Plus1;

let ss = SieveN2Plus1::new(10);
let primes: Vec<_> = ss.primes().collect();
assert_eq!(primes, [2, 5, 17, 37, 101]);
source

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

$n^2+1$ が素数のとき真を返す。

Examples
use nekolib::math::SieveN2Plus1;

let ss = SieveN2Plus1::new(10);
assert!(ss.is_prime(2));  // 5 is prime
assert!(!ss.is_prime(5));  // 26 = 2 * 13
source

pub fn factors(&self, n: usize) -> impl Iterator<Item = (usize, u32)> + '_

$n^2+1$ を素因数分解する。

底の昇順とは限らないので注意。最小の反例は n = 21

Examples
use nekolib::math::SieveN2Plus1;

let ss = SieveN2Plus1::new(10);
assert_eq!(ss.factors(0).next(), None);
assert_eq!(ss.factors(4).collect::<Vec<_>>(), [(17, 1)]);
assert_eq!(ss.factors(7).collect::<Vec<_>>(), [(2, 1), (5, 2)]);
source

pub fn factors_dup(&self, n: usize) -> impl Iterator<Item = usize> + '_

$n^2+1$ を素因数を列挙する。重複あり。

底の昇順とは限らないので注意。最小の反例は n = 21

Examples
use nekolib::math::SieveN2Plus1;

let ss = SieveN2Plus1::new(10);
assert_eq!(ss.factors_dup(0).next(), None);
assert_eq!(ss.factors_dup(4).collect::<Vec<_>>(), [17]);
assert_eq!(ss.factors_dup(7).collect::<Vec<_>>(), [2, 5, 5]);

Trait Implementations§

source§

impl Clone for SieveN2Plus1

source§

fn clone(&self) -> SieveN2Plus1

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for SieveN2Plus1

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V