1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
use integer::Integer;
use malachite_base::num::arithmetic::traits::{Parity, Pow, PowAssign};
impl Pow<u64> for Integer {
type Output = Integer;
/// Raises an [`Integer`] to a power, taking the [`Integer`] by value.
///
/// $f(x, n) = x^n$.
///
/// # Worst-case complexity
/// $T(n, m) = O(nm \log (nm) \log\log (nm))$
///
/// $M(n, m) = O(nm \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $m$ is
/// `exp`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::Pow;
/// use malachite_nz::integer::Integer;
/// use std::str::FromStr;
///
/// assert_eq!(
/// Integer::from(-3).pow(100).to_string(),
/// "515377520732011331036461129765621272702107522001"
/// );
/// assert_eq!(
/// Integer::from_str("-12345678987654321").unwrap().pow(3).to_string(),
/// "-1881676411868862234942354805142998028003108518161"
/// );
/// ```
#[inline]
fn pow(mut self, exp: u64) -> Integer {
self.pow_assign(exp);
self
}
}
impl<'a> Pow<u64> for &'a Integer {
type Output = Integer;
/// Raises an [`Integer`] to a power, taking the [`Integer`] by reference.
///
/// $f(x, n) = x^n$.
///
/// # Worst-case complexity
/// $T(n, m) = O(nm \log (nm) \log\log (nm))$
///
/// $M(n, m) = O(nm \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $m$ is
/// `exp`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::Pow;
/// use malachite_nz::integer::Integer;
/// use std::str::FromStr;
///
/// assert_eq!(
/// (&Integer::from(-3)).pow(100).to_string(),
/// "515377520732011331036461129765621272702107522001"
/// );
/// assert_eq!(
/// (&Integer::from_str("-12345678987654321").unwrap()).pow(3).to_string(),
/// "-1881676411868862234942354805142998028003108518161"
/// );
/// ```
#[inline]
fn pow(self, exp: u64) -> Integer {
Integer {
sign: exp.even() || self.sign,
abs: (&self.abs).pow(exp),
}
}
}
impl PowAssign<u64> for Integer {
/// Raises an [`Integer`] to a power in place.
///
/// $x \gets x^n$.
///
/// # Worst-case complexity
/// $T(n, m) = O(nm \log (nm) \log\log (nm))$
///
/// $M(n, m) = O(nm \log (nm))$
///
/// where $T$ is time, $M$ is additional memory, $n$ is `self.significant_bits()`, and $m$ is
/// `exp`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::PowAssign;
/// use malachite_nz::integer::Integer;
/// use std::str::FromStr;
///
/// let mut x = Integer::from(-3);
/// x.pow_assign(100);
/// assert_eq!(x.to_string(), "515377520732011331036461129765621272702107522001");
///
/// let mut x = Integer::from_str("-12345678987654321").unwrap();
/// x.pow_assign(3);
/// assert_eq!(x.to_string(), "-1881676411868862234942354805142998028003108518161");
/// ```
fn pow_assign(&mut self, exp: u64) {
self.sign = self.sign || exp.even();
self.abs.pow_assign(exp);
}
}