pub struct ModNum<N> { /* private fields */ }
Expand description

Represents an integer a (mod m)

Implementations§

source§

impl<N: NumType> ModNum<N>

source

pub fn new(a: N, m: N) -> Self

Creates a new integer a (mod m)

source

pub fn iter(&self) -> ModNumIterator<N, Self>

Returns an iterator starting at a (mod m) and ending at a - 1 (mod m)

source§

impl<N: NumType + Signed> ModNum<N>

source

pub fn chinese_remainder(&self, other: ModNum<N>) -> ModNum<N>

Solves a pair of modular equations using the Chinese Remainder Theorem.

This is my translation into Rust of Brent Yorgey’s Haskell implementation.

  • self represents the modular equation x = a (mod m)
  • other represents the modular equation x = b (mod n)
  • It returns a ModNum corresponding to the equation x = c (mod mn) where c is congruent both to a (mod m) and b (mod n)
source

pub fn chinese_remainder_system<I: Iterator<Item = ModNum<N>>>( modnums: I ) -> Option<ModNum<N>>

Solves a system of modular equations using ModMum::chinese_remainder().

Each equation in the system is an element of the modnums iterator parameter.

  • Returns None if the iterator is empty.
  • Returns Some(element) if the iterator has only one element.
  • Returns Some(solution) if the iterator has two or more elements, where the solution is found by repeatedly calling ModNum::chinese_remainder().
source§

impl<N: NumType + Signed> ModNum<N>

Exponentiates safely with negative exponents - if the inverse is undefined, it returns None, otherwise it returns Some(self.pow(rhs)).

source

pub fn pow_signed(&self, rhs: N) -> Option<Self>

Trait Implementations§

source§

impl<N: NumType> Add<ModNum<N>> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<N: NumType> Add<N> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the + operator.
source§

fn add(self, rhs: N) -> Self::Output

Performs the + operation. Read more
source§

impl<N: NumType> AddAssign<ModNum<N>> for ModNum<N>

source§

fn add_assign(&mut self, rhs: ModNum<N>)

Performs the += operation. Read more
source§

impl<N: NumType> AddAssign<N> for ModNum<N>

source§

fn add_assign(&mut self, rhs: N)

Performs the += operation. Read more
source§

impl<N: Clone> Clone for ModNum<N>

source§

fn clone(&self) -> ModNum<N>

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<N: Debug> Debug for ModNum<N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<N: NumType> Display for ModNum<N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<N: NumType + Signed> Div<ModNum<N>> for ModNum<N>

§

type Output = Option<ModNum<N>>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
source§

impl<N: NumType + Signed> Div<N> for ModNum<N>

§

type Output = Option<ModNum<N>>

The resulting type after applying the / operator.
source§

fn div(self, rhs: N) -> Self::Output

Performs the / operation. Read more
source§

impl<N: NumType + Signed> DivAssign<ModNum<N>> for ModNum<N>

source§

fn div_assign(&mut self, rhs: ModNum<N>)

Performs the /= operation. Read more
source§

impl<N: NumType + Signed> DivAssign<N> for ModNum<N>

source§

fn div_assign(&mut self, rhs: N)

Performs the /= operation. Read more
source§

impl<N: Hash> Hash for ModNum<N>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<N: NumType> MNum for ModNum<N>

§

type Num = N

source§

fn a(&self) -> N

source§

fn m(&self) -> N

source§

fn with(&self, new_a: Self::Num) -> Self

source§

fn replace(&mut self, new_a: Self::Num)

source§

impl<N: NumType> Mul<ModNum<N>> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<N: NumType> Mul<N> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: N) -> Self::Output

Performs the * operation. Read more
source§

impl<N: NumType> MulAssign<ModNum<N>> for ModNum<N>

source§

fn mul_assign(&mut self, rhs: ModNum<N>)

Performs the *= operation. Read more
source§

impl<N: NumType> MulAssign<N> for ModNum<N>

source§

fn mul_assign(&mut self, rhs: N)

Performs the *= operation. Read more
source§

impl<N: NumType> Neg for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<N: Ord> Ord for ModNum<N>

source§

fn cmp(&self, other: &ModNum<N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<N: PartialEq> PartialEq<ModNum<N>> for ModNum<N>

source§

fn eq(&self, other: &ModNum<N>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<N: NumType> PartialEq<N> for ModNum<N>

Returns true if other is congruent to self.a() (mod self.m())

source§

fn eq(&self, other: &N) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<N: PartialOrd> PartialOrd<ModNum<N>> for ModNum<N>

source§

fn partial_cmp(&self, other: &ModNum<N>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<N: NumType> PartialOrd<N> for ModNum<N>

source§

fn partial_cmp(&self, other: &N) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<N: NumType> Pow<ModNum<N>> for ModNum<N>

§

type Output = ModNum<N>

The result after applying the operator.
source§

fn pow(self, rhs: Self) -> Self::Output

Returns self to the power rhs. Read more
source§

impl<N: NumType> Pow<N> for ModNum<N>

source§

fn pow(self, rhs: N) -> Self::Output

Returns a^rhs (mod m), for rhs >= 0. Implements efficient modular exponentiation by repeated squaring.

Panics if rhs < 0. If negative exponents are possible, use .pow_signed()

§

type Output = ModNum<N>

The result after applying the operator.
source§

impl<N: NumType> SaturatingAdd for ModNum<N>

source§

fn saturating_add(&self, v: &Self) -> Self

Saturating addition. Computes self + other, saturating at the relevant high or low boundary of the type.
source§

impl<N: NumType> SaturatingSub for ModNum<N>

source§

fn saturating_sub(&self, v: &Self) -> Self

Saturating subtraction. Computes self - other, saturating at the relevant high or low boundary of the type.
source§

impl<N: NumType> Sub<ModNum<N>> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<N: NumType> Sub<N> for ModNum<N>

§

type Output = ModNum<N>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: N) -> Self::Output

Performs the - operation. Read more
source§

impl<N: NumType> SubAssign<ModNum<N>> for ModNum<N>

source§

fn sub_assign(&mut self, rhs: ModNum<N>)

Performs the -= operation. Read more
source§

impl<N: NumType> SubAssign<N> for ModNum<N>

source§

fn sub_assign(&mut self, rhs: N)

Performs the -= operation. Read more
source§

impl<N: Copy> Copy for ModNum<N>

source§

impl<N: Eq> Eq for ModNum<N>

source§

impl<N> StructuralEq for ModNum<N>

source§

impl<N> StructuralPartialEq for ModNum<N>

Auto Trait Implementations§

§

impl<N> RefUnwindSafe for ModNum<N>where N: RefUnwindSafe,

§

impl<N> Send for ModNum<N>where N: Send,

§

impl<N> Sync for ModNum<N>where N: Sync,

§

impl<N> Unpin for ModNum<N>where N: Unpin,

§

impl<N> UnwindSafe for ModNum<N>where N: UnwindSafe,

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · 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.
const: unstable · source§

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

Performs the conversion.