pub trait MNum: Copy + Eq + PartialEq {
    type Num: NumType;

    // Required methods
    fn a(&self) -> Self::Num;
    fn m(&self) -> Self::Num;
    fn with(&self, new_a: Self::Num) -> Self;

    // Provided methods
    fn replace(&mut self, new_a: Self::Num) { ... }
    fn egcd(a: Self::Num, b: Self::Num) -> (Self::Num, Self::Num, Self::Num)
       where Self::Num: Signed { ... }
    fn inverse(&self) -> Option<Self>
       where Self::Num: Signed { ... }
}

Required Associated Types§

Required Methods§

source

fn a(&self) -> Self::Num

source

fn m(&self) -> Self::Num

source

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

Provided Methods§

source

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

source

fn egcd(a: Self::Num, b: Self::Num) -> (Self::Num, Self::Num, Self::Num)where Self::Num: Signed,

Extended Euclidean Algorithm for Greatest Common Divisor for GCD.

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

Given two integers a and b, it returns three integer values:

  • Greatest Common Divisor (g) of a and b
  • Two additional values x and y, where ax + by = g
source

fn inverse(&self) -> Option<Self>where Self::Num: Signed,

Returns the modular inverse, if it exists. Returns None if it does not exist.

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

Let m = ModNum::new(a, m), where a and m are relatively prime. Then m * m.inverse().unwrap().a() is congruent to 1 (mod m).

Returns None if a and m are not relatively prime.

Implementors§

source§

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

§

type Num = N

source§

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

§

type Num = N

source§

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

§

type Num = N

source§

impl<N: NumType, const M: usize> MNum for ModNumC<N, M>

§

type Num = N

source§

impl<N: NumType, const M: usize> MNum for WrapCountNumC<N, M>

§

type Num = N

source§

impl<N: NumType, const M: usize, const O: isize> MNum for OffsetNumC<N, M, O>

§

type Num = N