clippy: fix the more complex warnings

This commit is contained in:
Sylvestre Ledru
2026-02-18 21:09:06 +01:00
parent 0369f28e0d
commit 3704abf843
8 changed files with 40 additions and 112 deletions
+3 -7
View File
@@ -189,7 +189,7 @@ pub fn bench_factorization(c: &mut Criterion) {
group.bench_function("num-prime (this crate)", |b| {
b.iter(|| {
numbers()
.filter(|&n| nt_funcs::factorize64(n as u64).len() > 1)
.filter(|&n| nt_funcs::factorize64(n).len() > 1)
.count()
})
});
@@ -213,9 +213,7 @@ pub fn bench_prime_gen(c: &mut Criterion) {
b.iter(|| -> num_bigint::BigUint { rng.gen_prime(256, None) })
});
group.bench_function("num-primes", |b| b.iter(|| Generator::new_prime(256)));
group.bench_function("glass_pumpkin", |b| {
b.iter(|| gprime::from_rng(256, &mut rng))
});
group.bench_function("glass_pumpkin", |b| b.iter(|| gprime::new(256)));
group.finish();
let mut group = c.benchmark_group("safe prime generation (256 bits)");
@@ -226,9 +224,7 @@ pub fn bench_prime_gen(c: &mut Criterion) {
b.iter(|| -> num_bigint::BigUint { rng.gen_safe_prime(256) })
});
group.bench_function("num-primes", |b| b.iter(|| Generator::safe_prime(256)));
group.bench_function("glass_pumpkin", |b| {
b.iter(|| safe_gprime::from_rng(256, &mut rng))
});
group.bench_function("glass_pumpkin", |b| b.iter(|| safe_gprime::new(256)));
group.finish();
}
+9 -52
View File
@@ -6,48 +6,6 @@ use num_prime::factor::{one_line, pollard_rho, squfof, SQUFOF_MULTIPLIERS};
use num_prime::RandPrime;
use rand::random;
/// Collect the the iteration number of each factorization algorithm with different settings
fn profile_n(n: u128) -> Vec<(String, usize)> {
let k_squfof: Vec<u16> = SQUFOF_MULTIPLIERS.iter().take(10).copied().collect();
let k_oneline: Vec<u16> = vec![1, 360, 480];
const MAXITER: usize = 1 << 20;
const POLLARD_REPEATS: usize = 2;
let mut n_stats = Vec::new();
// pollard rho
for i in 0..POLLARD_REPEATS {
n_stats.push((
format!("pollard_rho{}", i + 1),
pollard_rho(&n, random(), random(), MAXITER).1,
));
}
// squfof
for &k in &k_squfof {
let key = format!("squfof_k{k}");
if let Some(kn) = n.checked_mul(u128::from(k)) {
let n = squfof(&n, kn, MAXITER).1;
n_stats.push((key, n));
} else {
n_stats.push((key, MAXITER));
};
}
// one line
for &k in &k_oneline {
let key = format!("one_line_k{k}");
if let Some(kn) = n.checked_mul(u128::from(k)) {
let n = one_line(&n, kn, MAXITER).1;
n_stats.push((key, n));
} else {
n_stats.push((key, MAXITER));
};
}
n_stats
}
/// Collect the best case of each factorization algorithm
fn profile_n_min(n: u128) -> Vec<(String, usize)> {
let k_squfof: Vec<u16> = SQUFOF_MULTIPLIERS.to_vec();
@@ -120,27 +78,26 @@ fn main() -> Result<(), Error> {
let n = p1 * p2;
n_list.push((n, (n as f64).log2() as f32));
println!("Semiprime ({total_bits}bits): {n} = {p1} * {p2}");
// stats.push(profile_n(n));
stats.push(profile_n_min(n));
}
}
// Log into the CSV file
let mut fout = File::create("profile_stats.csv")?;
fout.write(b"n,n_bits")?;
fout.write_all(b"n,n_bits")?;
for k in stats[0].iter().map(|(k, _)| k) {
fout.write(b",")?;
fout.write(k.as_bytes())?;
fout.write_all(b",")?;
fout.write_all(k.as_bytes())?;
}
for ((n, bits), n_stats) in n_list.iter().zip(stats) {
fout.write(b"\n")?;
fout.write(n.to_string().as_bytes())?;
fout.write(b",")?;
fout.write(bits.to_string().as_bytes())?;
fout.write_all(b"\n")?;
fout.write_all(n.to_string().as_bytes())?;
fout.write_all(b",")?;
fout.write_all(bits.to_string().as_bytes())?;
for (_, v) in n_stats {
fout.write(b",")?;
fout.write(v.to_string().as_bytes())?;
fout.write_all(b",")?;
fout.write_all(v.to_string().as_bytes())?;
}
}
-3
View File
@@ -321,9 +321,6 @@ where
// https://github.com/zademn/facto-rs/
// https://github.com/elmomoilanen/prime-factorization
// https://cseweb.ucsd.edu/~ethome/teaching/2022-cse-291-14/
fn pollard_pp1() {}
fn williams_pp1() {}
#[cfg(test)]
mod tests {
use super::*;
+1 -1
View File
@@ -59,7 +59,7 @@
//! # Optional Features
//! - `big-int` (default): Enable this feature to support `num-bigint::BigUint` as integer inputs.
//! - `big-table` (default): Enable this feature to allow compiling large precomputed tables which
//! could improve the speed of various functions with the cost of larger memory footprint.
//! could improve the speed of various functions with the cost of larger memory footprint.
//!
pub mod buffer;
+1 -10
View File
@@ -94,15 +94,7 @@ impl<T: Integer + Clone, R: Reducer<T>> Eq for Mint<T, R> {}
impl<T: Integer + Clone, R: Reducer<T> + Clone> PartialOrd for Mint<T, R> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (&self.0, &other.0) {
(Left(v1), Left(v2)) => v1.partial_cmp(v2),
(Left(v1), Right(v2)) => v1.partial_cmp(&v2.residue()),
(Right(v1), Left(v2)) => v1.residue().partial_cmp(v2),
(Right(v1), Right(v2)) => {
debug_assert!(v1.modulus() == v2.modulus());
v1.residue().partial_cmp(&v2.residue())
}
}
Some(self.cmp(other))
}
}
impl<T: Integer + Clone, R: Reducer<T> + Clone> Ord for Mint<T, R> {
@@ -348,7 +340,6 @@ impl<T: Integer + Clone, R: Reducer<T> + Clone> Integer for Mint<T, R> {
forward_binops_left_ref_only!(div_floor);
forward_binops_left_ref_only!(mod_floor);
forward_binops_left_ref_only!(lcm);
forward_binops_left_ref_only!(divides => bool);
forward_binops_left_ref_only!(is_multiple_of => bool);
forward_uops_ref!(is_even => bool);
forward_uops_ref!(is_odd => bool);
+13 -14
View File
@@ -647,9 +647,9 @@ where
///
/// # Reference:
/// - \[1] Dusart, Pierre. "Estimates of Some Functions Over Primes without R.H."
/// [arxiv:1002.0442](http://arxiv.org/abs/1002.0442). 2010.
/// [arxiv:1002.0442](http://arxiv.org/abs/1002.0442). 2010.
/// - \[2] Dusart, Pierre. "Explicit estimates of some functions over primes."
/// The Ramanujan Journal 45.1 (2018): 227-251.
/// The Ramanujan Journal 45.1 (2018): 227-251.
pub fn prime_pi_bounds<T: ToPrimitive + FromPrimitive>(target: &T) -> (T, T) {
if let Some(x) = target.to_u64() {
// use existing primes and return exact value
@@ -719,15 +719,15 @@ pub fn prime_pi_bounds<T: ToPrimitive + FromPrimitive>(target: &T) -> (T, T) {
///
/// # Reference:
/// - \[1] Dusart, Pierre. "Estimates of Some Functions Over Primes without R.H."
/// arXiv preprint [arXiv:1002.0442](https://arxiv.org/abs/1002.0442) (2010).
/// arXiv preprint [arXiv:1002.0442](https://arxiv.org/abs/1002.0442) (2010).
/// - \[2] Rosser, J. Barkley, and Lowell Schoenfeld. "Approximate formulas for some
/// functions of prime numbers." Illinois Journal of Mathematics 6.1 (1962): 64-94.
/// functions of prime numbers." Illinois Journal of Mathematics 6.1 (1962): 64-94.
/// - \[3] Dusart, Pierre. "The k th prime is greater than k (ln k+ ln ln k-1) for k≥ 2."
/// Mathematics of computation (1999): 411-415.
/// Mathematics of computation (1999): 411-415.
/// - \[4] Axler, Christian. ["New Estimates for the nth Prime Number."](https://www.emis.de/journals/JIS/VOL22/Axler/axler17.pdf)
/// Journal of Integer Sequences 22.2 (2019): 3.
/// Journal of Integer Sequences 22.2 (2019): 3.
/// - \[5] Axler, Christian. [Uber die Primzahl-Zählfunktion, die n-te Primzahl und verallgemeinerte Ramanujan-Primzahlen. Diss.](http://docserv.uniduesseldorf.de/servlets/DerivateServlet/Derivate-28284/pdfa-1b.pdf)
/// `PhD` thesis, Düsseldorf, 2013.
/// `PhD` thesis, Düsseldorf, 2013.
///
/// Note that some of the results might depend on the Riemann Hypothesis. If you find
/// any prime that doesn't fall in the bound, then it might be a big discovery!
@@ -1186,7 +1186,6 @@ mod tests {
1523, 1619, 1823, 1907,
];
for p in SMALL_PRIMES {
let p = p;
if p > 1500 {
break;
}
@@ -1203,8 +1202,8 @@ mod tests {
let mu20: [i8; 20] = [
1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1, 0, -1, 0,
];
for i in 0..20 {
assert_eq!(moebius(&(i + 1)), mu20[i], "moebius on {i}");
for (i, &expected) in mu20.iter().enumerate() {
assert_eq!(moebius(&(i + 1)), expected, "moebius on {i}");
}
// some square numbers
@@ -1216,15 +1215,15 @@ mod tests {
30, 42, 66, 70, 78, 102, 105, 110, 114, 130, 138, 154, 165, 170, 174, 182, 186, 190,
195, 222,
]; // OEIS:A007304
for i in 0..20 {
assert_eq!(moebius(&sphenic3[i]), -1i8, "moebius on {}", sphenic3[i]);
for &val in &sphenic3 {
assert_eq!(moebius(&val), -1i8, "moebius on {val}");
}
let sphenic5: [u16; 23] = [
2310, 2730, 3570, 3990, 4290, 4830, 5610, 6006, 6090, 6270, 6510, 6630, 7410, 7590,
7770, 7854, 8610, 8778, 8970, 9030, 9282, 9570, 9690,
]; // OEIS:A046387
for i in 0..20 {
assert_eq!(moebius(&sphenic5[i]), -1i8, "moebius on {}", sphenic5[i]);
for &val in sphenic5.iter().take(20) {
assert_eq!(moebius(&val), -1i8, "moebius on {val}");
}
}
+3 -3
View File
@@ -458,14 +458,14 @@ mod tests {
2_600_190_307_441,
];
let m = random::<u16>();
for n in 2..p3qm1.len() {
for (n, &p3qm1_val) in p3qm1.iter().enumerate().skip(2) {
let (uk, _) = LucasUtils::lucasm(3, -1, u64::from(m), n as u64);
assert_eq!(uk, p3qm1[n] % u64::from(m));
assert_eq!(uk, p3qm1_val % u64::from(m));
#[cfg(feature = "num-bigint")]
{
let (uk, _) = LucasUtils::lucasm(3, -1, BigUint::from(m), BigUint::from(n));
assert_eq!(uk, BigUint::from(p3qm1[n] % (m as u64)));
assert_eq!(uk, BigUint::from(p3qm1_val % u64::from(m)));
}
}
+10 -22
View File
@@ -1,5 +1,5 @@
use core::default::Default;
use std::ops::{BitAnd, BitOr};
use std::ops::{BitAnd, BitOr, Mul};
use either::Either;
use num_integer::{Integer, Roots};
@@ -36,10 +36,7 @@ impl Primality {
#[inline(always)]
#[must_use]
pub fn probably(self) -> bool {
match self {
Primality::No => false,
_ => true,
}
!matches!(self, Primality::No)
}
}
@@ -54,7 +51,10 @@ impl BitAnd<Primality> for Primality {
Primality::Probable(p) => match rhs {
Primality::No => Primality::No,
Primality::Yes => Primality::Probable(p),
Primality::Probable(p2) => Primality::Probable(p * p2),
Primality::Probable(p2) => {
let combined = p.mul(p2);
Primality::Probable(combined)
}
},
}
}
@@ -130,11 +130,6 @@ impl PrimalityTestConfig {
eslprp_test: false,
}
}
/// Create a configuration for PSW test (base 2 SPRP + Fibonacci test)
fn psw() {
todo!() // TODO: implement Fibonacci PRP
}
}
/// Represents a configuration for integer factorization
@@ -150,12 +145,6 @@ pub struct FactorizationConfig {
/// Number of trials with Pollard's rho method
pub rho_trials: usize,
/// Number of trials with Pollard's p-1 method
pm1_trials: usize,
/// Number of trials with William's p+1 method
pp1_trials: usize,
}
impl Default for FactorizationConfig {
@@ -167,8 +156,6 @@ impl Default for FactorizationConfig {
primality_config: PrimalityTestConfig::default(),
td_limit: Some(THRESHOLD_DEFAULT_TD),
rho_trials: 4,
pm1_trials: 0,
pp1_trials: 0,
}
}
}
@@ -177,9 +164,10 @@ impl FactorizationConfig {
/// Same as the default configuration but with strict primality check
#[must_use]
pub fn strict() -> Self {
let mut config = Self::default();
config.primality_config = PrimalityTestConfig::strict();
config
Self {
primality_config: PrimalityTestConfig::strict(),
..Self::default()
}
}
}