malachite_base/num/factorization/traits.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9/// A trait for testing whether a number is prime.
10pub trait IsPrime {
11 fn is_prime(&self) -> bool;
12}
13
14/// A trait for testing whether a number is a square.
15pub trait IsSquare {
16 fn is_square(&self) -> bool;
17}
18
19/// A trait for testing whether a number is a perfect power.
20pub trait IsPower {
21 fn is_power(&self) -> bool;
22}
23
24/// A trait for expessing as number as the power of some number raised to an exponent greater than
25/// 1, if such a representation exists.
26pub trait ExpressAsPower: Sized {
27 fn express_as_power(&self) -> Option<(Self, u64)>;
28}
29
30/// A trait for removing the largest power of a factor from a number, returning the reduced number
31/// and how many times the factor was removed.
32pub trait RemovePower<RHS = Self> {
33 type Output;
34
35 fn remove_power(self, other: RHS) -> (Self::Output, u64);
36}
37
38/// A trait for replacing a number with itself divided by the largest power of a factor, returning
39/// how many times the factor was removed.
40pub trait RemovePowerAssign<RHS = Self> {
41 fn remove_power_assign(&mut self, other: RHS) -> u64;
42}
43
44/// A trait for finding the prime factorization of a number.
45pub trait Factor {
46 type FACTORS;
47
48 fn factor(&self) -> Self::FACTORS;
49}
50
51/// A trait for producing iterators of primes.
52pub trait Primes {
53 type I: Iterator<Item = Self>;
54 type LI: Iterator<Item = Self>;
55
56 fn primes_less_than(n: &Self) -> Self::LI;
57
58 fn primes_less_than_or_equal_to(n: &Self) -> Self::LI;
59
60 fn primes() -> Self::I;
61}
62
63/// A trait for finding a primitive root modulo a prime.
64pub trait PrimitiveRootPrime {
65 type Output;
66
67 fn primitive_root_prime(&self) -> Self::Output;
68}