Trait NumberUtils

Source
pub trait NumberUtils {
    // Required methods
    fn factorial(&self) -> Self;
    fn permutation(&self, k: u32) -> Self;
    fn combination(&self, k: u32) -> Self;
    fn gcd(&self, m: Self) -> Self;
    fn lcm(&self, m: Self) -> Self;
    fn isqrt(&self) -> Self;
    fn checked_factorial(&self) -> Option<Self>
       where Self: Sized;
    fn checked_permutation(&self, k: u32) -> Option<Self>
       where Self: Sized;
    fn checked_combination(&self, k: u32) -> Option<Self>
       where Self: Sized;
    fn digits(&self) -> Self;
}

Required Methods§

Source

fn factorial(&self) -> Self

Calculates factorial.

§Examples

Basic usage:

let n: u32 = 6;
assert_eq!(n.factorial(), 720);
Source

fn permutation(&self, k: u32) -> Self

Calculates k-permutations.

§Examples

Basic usage:

let n: u32 = 6;
assert_eq!(n.permutation(3), 120);
Source

fn combination(&self, k: u32) -> Self

Calculates k-combination.

§Examples

Basic usage:

let n: u32 = 6;
assert_eq!(n.combination(3), 20);
Source

fn gcd(&self, m: Self) -> Self

Calculates greatest common divisor of two integers.

§Examples

Basic usage:

let n: u32 = 54;
assert_eq!(n.gcd(24), 6);
Source

fn lcm(&self, m: Self) -> Self

Calculates least common multiple.

§Examples

Basic usage:

let n: u32 = 72;
assert_eq!(n.lcm(10), 360);
Source

fn isqrt(&self) -> Self

Calculates integer square root.

§Examples

Basic usage:

let n: u32 = 122;
assert_eq!(n.isqrt(), 11);
Source

fn checked_factorial(&self) -> Option<Self>
where Self: Sized,

Calculates factorial, returning None if overflow occured.

§Examples

Basic usage:

let n: u32 = 5;
assert_eq!(n.checked_factorial(), Some(120));
assert_eq!(u32::MAX.checked_factorial(), None);
Source

fn checked_permutation(&self, k: u32) -> Option<Self>
where Self: Sized,

Calculates k-permutations, returning None if overflow occured.

§Examples

Basic usage:

let n: u32 = 6;
assert_eq!(n.checked_permutation(3), Some(120));
assert_eq!(u32::MAX.checked_permutation(3), None);
Source

fn checked_combination(&self, k: u32) -> Option<Self>
where Self: Sized,

Calculates k-combination, returning None if overflow occured.

§Examples

Basic usage:

let n: u32 = 6;
assert_eq!(n.checked_combination(3), Some(20));
assert_eq!(u32::MAX.checked_combination(1200), None);
Source

fn digits(&self) -> Self

Calculates number of digits.

§Examples
let n: u32 = 1234567890;
assert_eq!(n.digits(), 10);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl NumberUtils for u32

Source§

fn factorial(&self) -> Self

Source§

fn permutation(&self, k: u32) -> Self

Source§

fn combination(&self, k: u32) -> Self

Source§

fn gcd(&self, m: Self) -> Self

Source§

fn lcm(&self, m: Self) -> Self

Source§

fn isqrt(&self) -> Self

Source§

fn checked_factorial(&self) -> Option<Self>

Source§

fn checked_permutation(&self, k: u32) -> Option<Self>

Source§

fn checked_combination(&self, k: u32) -> Option<Self>

Source§

fn digits(&self) -> Self

Source§

impl NumberUtils for u64

Source§

fn factorial(&self) -> Self

Source§

fn permutation(&self, k: u32) -> Self

Source§

fn combination(&self, k: u32) -> Self

Source§

fn gcd(&self, m: Self) -> Self

Source§

fn lcm(&self, m: Self) -> Self

Source§

fn isqrt(&self) -> Self

Source§

fn checked_factorial(&self) -> Option<Self>

Source§

fn checked_permutation(&self, k: u32) -> Option<Self>

Source§

fn checked_combination(&self, k: u32) -> Option<Self>

Source§

fn digits(&self) -> Self

Source§

impl NumberUtils for u128

Source§

fn factorial(&self) -> Self

Source§

fn permutation(&self, k: u32) -> Self

Source§

fn combination(&self, k: u32) -> Self

Source§

fn gcd(&self, m: Self) -> Self

Source§

fn lcm(&self, m: Self) -> Self

Source§

fn isqrt(&self) -> Self

Source§

fn checked_factorial(&self) -> Option<Self>

Source§

fn checked_permutation(&self, k: u32) -> Option<Self>

Source§

fn checked_combination(&self, k: u32) -> Option<Self>

Source§

fn digits(&self) -> Self

Source§

impl NumberUtils for usize

Source§

fn factorial(&self) -> Self

Source§

fn permutation(&self, k: u32) -> Self

Source§

fn combination(&self, k: u32) -> Self

Source§

fn gcd(&self, m: Self) -> Self

Source§

fn lcm(&self, m: Self) -> Self

Source§

fn isqrt(&self) -> Self

Source§

fn checked_factorial(&self) -> Option<Self>

Source§

fn checked_permutation(&self, k: u32) -> Option<Self>

Source§

fn checked_combination(&self, k: u32) -> Option<Self>

Source§

fn digits(&self) -> Self

Implementors§