mirror of
https://github.com/uutils/num-prime.git
synced 2026-06-10 16:12:35 -07:00
clippy: fix the obvious warnings
This commit is contained in:
+12
-9
@@ -211,10 +211,10 @@ pub trait PrimeBufferExt: for<'a> PrimeBuffer<'a> {
|
||||
};
|
||||
|
||||
for p in self.iter().map(|p| T::from_u64(*p).unwrap()) {
|
||||
if &p > &tsqrt {
|
||||
if p > tsqrt {
|
||||
return None; // the number is a prime
|
||||
}
|
||||
if &p > &limit {
|
||||
if p > limit {
|
||||
break;
|
||||
}
|
||||
if target.is_multiple_of(&p) {
|
||||
@@ -350,13 +350,13 @@ impl NaiveBuffer {
|
||||
// for endless prime iter. This can be a method in this trait, or standalone function,
|
||||
// or implement as IntoIter. We can try to implement PrimeBuffer on primal first and see
|
||||
// if it's reasonable to unifiy
|
||||
pub fn primes(&mut self, limit: u64) -> std::iter::Take<<Self as PrimeBuffer>::PrimeIter> {
|
||||
pub fn primes(&mut self, limit: u64) -> std::iter::Take<<Self as PrimeBuffer<'_>>::PrimeIter> {
|
||||
self.reserve(limit);
|
||||
let position = match self.list.binary_search(&limit) {
|
||||
Ok(p) => p + 1,
|
||||
Err(p) => p,
|
||||
}; // into_ok_or_err()
|
||||
return self.list.iter().take(position);
|
||||
self.list.iter().take(position)
|
||||
}
|
||||
|
||||
/// Returns all primes ≤ `limit` and takes ownership. The primes are sorted.
|
||||
@@ -372,7 +372,10 @@ impl NaiveBuffer {
|
||||
}
|
||||
|
||||
/// Returns primes of certain amount counting from 2. The primes are sorted.
|
||||
pub fn nprimes(&mut self, count: usize) -> std::iter::Take<<Self as PrimeBuffer>::PrimeIter> {
|
||||
pub fn nprimes(
|
||||
&mut self,
|
||||
count: usize,
|
||||
) -> std::iter::Take<<Self as PrimeBuffer<'_>>::PrimeIter> {
|
||||
let (_, bound) = nth_prime_bounds(&(count as u64))
|
||||
.expect("Estimated size of the largest prime will be larger than u64 limit");
|
||||
self.reserve(bound);
|
||||
@@ -403,7 +406,7 @@ impl NaiveBuffer {
|
||||
// Directly sieve if the limit is small
|
||||
const THRESHOLD_NTH_PRIME_SIEVE: u64 = 4096;
|
||||
if n <= THRESHOLD_NTH_PRIME_SIEVE {
|
||||
return *self.nprimes(n as usize).last().unwrap();
|
||||
return *self.nprimes(n as usize).next_back().unwrap();
|
||||
}
|
||||
|
||||
// Check primes starting from estimation
|
||||
@@ -424,7 +427,7 @@ impl NaiveBuffer {
|
||||
/// Legendre's phi function, used as a helper function for [`Self::prime_pi`]
|
||||
pub fn prime_phi(&mut self, x: u64, a: usize, cache: &mut LruCache<(u64, usize), u64>) -> u64 {
|
||||
if a == 1 {
|
||||
return (x + 1) / 2;
|
||||
return x.div_ceil(2);
|
||||
}
|
||||
if let Some(v) = cache.get(&(x, a)) {
|
||||
return *v;
|
||||
@@ -510,9 +513,9 @@ mod tests {
|
||||
pb.clear();
|
||||
assert_eq!(pb.primes(293).copied().collect::<Vec<_>>(), PRIME300);
|
||||
pb = NaiveBuffer::new();
|
||||
assert_eq!(*pb.primes(257).last().unwrap(), 257); // boundary of small table
|
||||
assert_eq!(*pb.primes(257).next_back().unwrap(), 257); // boundary of small table
|
||||
pb = NaiveBuffer::new();
|
||||
assert_eq!(*pb.primes(8167).last().unwrap(), 8167); // boundary of large table
|
||||
assert_eq!(*pb.primes(8167).next_back().unwrap(), 8167); // boundary of large table
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+2
-2
@@ -42,10 +42,10 @@ where
|
||||
let mut result = BTreeMap::new();
|
||||
let mut factored = false;
|
||||
for (p, pt) in primes.map(|p| (p, T::from_u64(p).unwrap())) {
|
||||
if &pt > &tsqrt {
|
||||
if pt > tsqrt {
|
||||
factored = true;
|
||||
}
|
||||
if &pt > &limit {
|
||||
if pt > limit {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -36,11 +36,11 @@ impl BitTest for BigUint {
|
||||
self.bit(position as u64)
|
||||
}
|
||||
fn bits(&self) -> usize {
|
||||
BigUint::bits(&self) as usize
|
||||
BigUint::bits(self) as usize
|
||||
}
|
||||
#[inline]
|
||||
fn trailing_zeros(&self) -> usize {
|
||||
match BigUint::trailing_zeros(&self) {
|
||||
match BigUint::trailing_zeros(self) {
|
||||
Some(a) => a as usize,
|
||||
None => 0,
|
||||
}
|
||||
@@ -116,7 +116,7 @@ impl ExactRoots for BigUint {
|
||||
|
||||
// check mod 2
|
||||
let shift = self.trailing_zeros().unwrap();
|
||||
if shift % 3 != 0 {
|
||||
if !shift.is_multiple_of(3) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
+6
-8
@@ -239,8 +239,8 @@ impl<T: Integer + Clone, R: Reducer<T>> Div<Mint<T, R>> for &Mint<T, R> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, 'b, T: Integer + Clone + for<'r> Div<&'r T, Output = T>, R: Reducer<T>> Div<&'b Mint<T, R>>
|
||||
for &'a Mint<T, R>
|
||||
impl<T: Integer + Clone + for<'r> Div<&'r T, Output = T>, R: Reducer<T>> Div<&Mint<T, R>>
|
||||
for &Mint<T, R>
|
||||
{
|
||||
type Output = Mint<T, R>;
|
||||
#[inline]
|
||||
@@ -297,7 +297,7 @@ impl<T: Integer + Clone, R: Reducer<T> + Clone> Rem<Mint<T, R>> for &Mint<T, R>
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, 'b, T: Integer + Clone, R: Reducer<T> + Clone> Rem<&'b Mint<T, R>> for &'a Mint<T, R> {
|
||||
impl<T: Integer + Clone, R: Reducer<T> + Clone> Rem<&Mint<T, R>> for &Mint<T, R> {
|
||||
type Output = Mint<T, R>;
|
||||
|
||||
#[inline]
|
||||
@@ -518,8 +518,8 @@ impl<T: Integer + Clone, R: Reducer<T> + Clone> ModularCoreOps<&Self, &Self> for
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<'a, 'b, T: Integer + Clone, R: Reducer<T> + Clone>
|
||||
ModularCoreOps<&'b Mint<T, R>, &'b Mint<T, R>> for &'a Mint<T, R>
|
||||
impl<'b, T: Integer + Clone, R: Reducer<T> + Clone> ModularCoreOps<&'b Mint<T, R>, &'b Mint<T, R>>
|
||||
for &Mint<T, R>
|
||||
{
|
||||
type Output = Mint<T, R>;
|
||||
#[inline]
|
||||
@@ -593,9 +593,7 @@ impl<T: Integer + Clone, R: Reducer<T> + Clone> ModularUnaryOps<&Self> for Mint<
|
||||
}))
|
||||
}
|
||||
}
|
||||
impl<'a, 'b, T: Integer + Clone, R: Reducer<T> + Clone> ModularUnaryOps<&'b Mint<T, R>>
|
||||
for &'a Mint<T, R>
|
||||
{
|
||||
impl<T: Integer + Clone, R: Reducer<T> + Clone> ModularUnaryOps<&Mint<T, R>> for &Mint<T, R> {
|
||||
type Output = Mint<T, R>;
|
||||
#[inline]
|
||||
fn negm(self, m: &Mint<T, R>) -> Self::Output {
|
||||
|
||||
+3
-3
@@ -308,7 +308,7 @@ pub(crate) fn factorize64_advanced(cofactors: &[(u64, usize)]) -> Vec<(u64, usiz
|
||||
i += 1;
|
||||
|
||||
// increase max iterations after trying all methods
|
||||
if i % NMETHODS == 0 {
|
||||
if i.is_multiple_of(NMETHODS) {
|
||||
max_iter_ratio *= 2;
|
||||
}
|
||||
};
|
||||
@@ -470,7 +470,7 @@ pub(crate) fn factorize128_advanced(cofactors: &[(u128, usize)]) -> Vec<(u128, u
|
||||
i += 1;
|
||||
|
||||
// increase max iterations after trying all methods
|
||||
if i % NMETHODS == 0 {
|
||||
if i.is_multiple_of(NMETHODS) {
|
||||
max_iter_ratio *= 2;
|
||||
}
|
||||
};
|
||||
@@ -624,7 +624,7 @@ where
|
||||
pub fn moebius_factorized<T>(factors: &BTreeMap<T, usize>) -> i8 {
|
||||
if factors.values().any(|exp| exp > &1) {
|
||||
0
|
||||
} else if factors.len() % 2 == 0 {
|
||||
} else if factors.len().is_multiple_of(2) {
|
||||
1
|
||||
} else {
|
||||
-1
|
||||
|
||||
+1
-1
@@ -87,7 +87,7 @@ where
|
||||
let mut neg = false;
|
||||
loop {
|
||||
// check if n is a square number after several trials
|
||||
if &d == &T::from_u8(13).unwrap() && (*n).is_square() {
|
||||
if d == T::from_u8(13).unwrap() && (*n).is_square() {
|
||||
break (0, 0);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1158,7 +1158,7 @@ pub const MILLER_RABIN_BASE32: [u16; 256] = [
|
||||
];
|
||||
|
||||
#[cfg(feature = "big-table")]
|
||||
pub const MILLER_RABIN_BASE64: [u32; 16384] = [
|
||||
pub static MILLER_RABIN_BASE64: [u32; 16384] = [
|
||||
0x0024_b047,
|
||||
0x002e_32a1,
|
||||
0x0038_b06b,
|
||||
|
||||
Reference in New Issue
Block a user