Module malachite_base::num::arithmetic::mod_pow
source · Expand description
Traits for raising a number to a power modulo another number.
The traits are ModPow
, ModPowAssign
, and
ModPowPrecomputed
.
ModPowPrecomputed
is useful when having to make several
exponentiations modulo the same modulus.
§mod_pow
use malachite_base::num::arithmetic::traits::ModPow;
assert_eq!(4u16.mod_pow(13, 497), 445);
assert_eq!(10u32.mod_pow(1000, 30), 10);
§mod_pow_assign
use malachite_base::num::arithmetic::traits::ModPowAssign;
let mut n = 4u16;
n.mod_pow_assign(13, 497);
assert_eq!(n, 445);
let mut n = 10u32;
n.mod_pow_assign(1000, 30);
assert_eq!(n, 10);
§mod_pow_precomputed
use malachite_base::num::arithmetic::traits::ModPowPrecomputed;
let data = u32::precompute_mod_pow_data(&497);
assert_eq!(4u32.mod_pow_precomputed(13, 497, &data), 445);
assert_eq!(5u32.mod_pow_precomputed(3, 497, &data), 125);
assert_eq!(4u32.mod_pow_precomputed(100, 497, &data), 116);
let data = u64::precompute_mod_pow_data(&30);
assert_eq!(10u64.mod_pow_precomputed(1000, 30, &data), 10);
assert_eq!(4u64.mod_pow_precomputed(9, 30, &data), 4);
assert_eq!(5u64.mod_pow_precomputed(8, 30, &data), 25);
let data = u16::precompute_mod_pow_data(&497);
assert_eq!(4u16.mod_pow_precomputed(13, 497, &data), 445);
assert_eq!(5u16.mod_pow_precomputed(3, 497, &data), 125);
assert_eq!(4u16.mod_pow_precomputed(100, 497, &data), 116);
let data = u8::precompute_mod_pow_data(&30);
assert_eq!(10u8.mod_pow_precomputed(1000, 30, &data), 10);
assert_eq!(4u8.mod_pow_precomputed(9, 30, &data), 4);
assert_eq!(5u8.mod_pow_precomputed(8, 30, &data), 25);
let data = u128::precompute_mod_pow_data(&497);
assert_eq!(4u128.mod_pow_precomputed(13, 497, &data), 445);
assert_eq!(5u128.mod_pow_precomputed(3, 497, &data), 125);
assert_eq!(4u128.mod_pow_precomputed(100, 497, &data), 116);
let data = u128::precompute_mod_pow_data(&30);
assert_eq!(10u128.mod_pow_precomputed(1000, 30, &data), 10);
assert_eq!(4u128.mod_pow_precomputed(9, 30, &data), 4);
assert_eq!(5u128.mod_pow_precomputed(8, 30, &data), 25);
§mod_pow_precomputed_assign
use malachite_base::num::arithmetic::traits::{ModPowPrecomputed, ModPowPrecomputedAssign};
let data = u32::precompute_mod_pow_data(&497);
let mut x = 4u32;
x.mod_pow_precomputed_assign(13, 497, &data);
assert_eq!(x, 445);
let mut x = 5u32;
x.mod_pow_precomputed_assign(3, 497, &data);
assert_eq!(x, 125);
let mut x = 4u32;
x.mod_pow_precomputed_assign(100, 497, &data);
assert_eq!(x, 116);
let data = u64::precompute_mod_pow_data(&30);
let mut x = 10u64;
x.mod_pow_precomputed_assign(1000, 30, &data);
assert_eq!(x, 10);
let mut x = 4u64;
x.mod_pow_precomputed_assign(9, 30, &data);
assert_eq!(x, 4);
let mut x = 5u64;
x.mod_pow_precomputed_assign(8, 30, &data);
assert_eq!(x, 25);