Skip to main content

ModDiv

Trait ModDiv 

Source
pub trait ModDiv<RHS = Self, M = Self> {
    type Output;

    // Required method
    fn mod_div(self, other: RHS, m: M) -> Option<Self::Output>;
}
Expand description

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

If the divisor is not invertible modulo $m$, a quotient may exist without being unique; in that case one of the quotients is returned.

Required Associated Types§

Required Methods§

Source

fn mod_div(self, other: RHS, m: M) -> Option<Self::Output>

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ModDiv for u8

Source§

fn mod_div(self, other: u8, m: u8) -> Option<u8>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = u8

Source§

impl ModDiv for u16

Source§

fn mod_div(self, other: u16, m: u16) -> Option<u16>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = u16

Source§

impl ModDiv for u32

Source§

fn mod_div(self, other: u32, m: u32) -> Option<u32>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = u32

Source§

impl ModDiv for u64

Source§

fn mod_div(self, other: u64, m: u64) -> Option<u64>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = u64

Source§

impl ModDiv for u128

Source§

fn mod_div(self, other: u128, m: u128) -> Option<u128>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = u128

Source§

impl ModDiv for usize

Source§

fn mod_div(self, other: usize, m: usize) -> Option<usize>

Divides a number by another number modulo a third number $m$, returning None if no quotient exists. The inputs must be already reduced modulo $m$.

A quotient exists if and only if $\gcd(y, m)$ divides $x$. If $y$ is not invertible modulo $m$, the quotient is not unique; all quotients differ by multiples of $m/\gcd(y, m)$, and this function returns one of them.

$f(x, y, m) = \operatorname{Some}(q)$, where $x, y, q < m$ and $qy \equiv x \mod m$, if such a $q$ exists.

§Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is m.significant_bits(): the extended Euclidean algorithm on words performs $O(n)$ iterations of constant-cost word operations, with no allocation.

§Panics

Panics if self or other are greater than or equal to m.

§Examples

See here.

Source§

type Output = usize

Implementors§