Module malachite_base::num::arithmetic::mod_mul
source · Expand description
Traits for multiplying two numbers modulo another number.
The traits are ModMul
, ModMulAssign
,
ModMulPrecomputed
, and
ModMulPrecomputedAssign
.
ModMulPrecomputed
and
ModMulPrecomputedAssign
are useful when having to make
several multiplications modulo the same modulus.
§mod_mul
use malachite_base::num::arithmetic::traits::ModMul;
assert_eq!(2u8.mod_mul(3, 7), 6);
assert_eq!(7u32.mod_mul(3, 10), 1);
§mod_mul_assign
use malachite_base::num::arithmetic::traits::ModMulAssign;
let mut n = 2u8;
n.mod_mul_assign(3, 7);
assert_eq!(n, 6);
let mut n = 7u32;
n.mod_mul_assign(3, 10);
assert_eq!(n, 1);
§mod_mul_precomputed
use malachite_base::num::arithmetic::traits::ModMulPrecomputed;
let data = u32::precompute_mod_mul_data(&7);
assert_eq!(2u32.mod_mul_precomputed(3, 7, &data), 6);
assert_eq!(5u32.mod_mul_precomputed(3, 7, &data), 1);
assert_eq!(4u32.mod_mul_precomputed(4, 7, &data), 2);
let data = u64::precompute_mod_mul_data(&10);
assert_eq!(7u64.mod_mul_precomputed(3, 10, &data), 1);
assert_eq!(4u64.mod_mul_precomputed(9, 10, &data), 6);
assert_eq!(5u64.mod_mul_precomputed(8, 10, &data), 0);
let data = u8::precompute_mod_mul_data(&7);
assert_eq!(2u8.mod_mul_precomputed(3, 7, &data), 6);
assert_eq!(5u8.mod_mul_precomputed(3, 7, &data), 1);
assert_eq!(4u8.mod_mul_precomputed(4, 7, &data), 2);
let data = u16::precompute_mod_mul_data(&10);
assert_eq!(7u16.mod_mul_precomputed(3, 10, &data), 1);
assert_eq!(4u16.mod_mul_precomputed(9, 10, &data), 6);
assert_eq!(5u16.mod_mul_precomputed(8, 10, &data), 0);
let data = u128::precompute_mod_mul_data(&7);
assert_eq!(2u128.mod_mul_precomputed(3, 7, &data), 6);
assert_eq!(5u128.mod_mul_precomputed(3, 7, &data), 1);
assert_eq!(4u128.mod_mul_precomputed(4, 7, &data), 2);
let data = u128::precompute_mod_mul_data(&10);
assert_eq!(7u128.mod_mul_precomputed(3, 10, &data), 1);
assert_eq!(4u128.mod_mul_precomputed(9, 10, &data), 6);
assert_eq!(5u128.mod_mul_precomputed(8, 10, &data), 0);
§mod_mul_precomputed_assign
use malachite_base::num::arithmetic::traits::{ModMulPrecomputed, ModMulPrecomputedAssign};
let data = u8::precompute_mod_mul_data(&7);
let mut x = 2u8;
x.mod_mul_precomputed_assign(3, 7, &data);
assert_eq!(x, 6);
let mut x = 5u8;
x.mod_mul_precomputed_assign(3, 7, &data);
assert_eq!(x, 1);
let mut x = 4u8;
x.mod_mul_precomputed_assign(4, 7, &data);
assert_eq!(x, 2);
let data = u32::precompute_mod_mul_data(&10);
let mut x = 7u32;
x.mod_mul_precomputed_assign(3, 10, &data);
assert_eq!(x, 1);
let mut x = 4u32;
x.mod_mul_precomputed_assign(9, 10, &data);
assert_eq!(x, 6);
let mut x = 5u32;
x.mod_mul_precomputed_assign(8, 10, &data);
assert_eq!(x, 0);