diff --git a/src/buffer.rs b/src/buffer.rs index 11402f0..1d07aaa 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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<::PrimeIter> { + pub fn primes(&mut self, limit: u64) -> std::iter::Take<>::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<::PrimeIter> { + pub fn nprimes( + &mut self, + count: usize, + ) -> std::iter::Take<>::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::>(), 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] diff --git a/src/factor.rs b/src/factor.rs index 81f475a..c58fcbb 100644 --- a/src/factor.rs +++ b/src/factor.rs @@ -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; } diff --git a/src/integer.rs b/src/integer.rs index f665686..cb9fd37 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -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; } diff --git a/src/mint.rs b/src/mint.rs index dd1dd88..5c3e438 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -239,8 +239,8 @@ impl> Div> for &Mint { } } } -impl<'a, 'b, T: Integer + Clone + for<'r> Div<&'r T, Output = T>, R: Reducer> Div<&'b Mint> - for &'a Mint +impl Div<&'r T, Output = T>, R: Reducer> Div<&Mint> + for &Mint { type Output = Mint; #[inline] @@ -297,7 +297,7 @@ impl + Clone> Rem> for &Mint } } } -impl<'a, 'b, T: Integer + Clone, R: Reducer + Clone> Rem<&'b Mint> for &'a Mint { +impl + Clone> Rem<&Mint> for &Mint { type Output = Mint; #[inline] @@ -518,8 +518,8 @@ impl + Clone> ModularCoreOps<&Self, &Self> for } } } -impl<'a, 'b, T: Integer + Clone, R: Reducer + Clone> - ModularCoreOps<&'b Mint, &'b Mint> for &'a Mint +impl<'b, T: Integer + Clone, R: Reducer + Clone> ModularCoreOps<&'b Mint, &'b Mint> + for &Mint { type Output = Mint; #[inline] @@ -593,9 +593,7 @@ impl + Clone> ModularUnaryOps<&Self> for Mint< })) } } -impl<'a, 'b, T: Integer + Clone, R: Reducer + Clone> ModularUnaryOps<&'b Mint> - for &'a Mint -{ +impl + Clone> ModularUnaryOps<&Mint> for &Mint { type Output = Mint; #[inline] fn negm(self, m: &Mint) -> Self::Output { diff --git a/src/nt_funcs.rs b/src/nt_funcs.rs index 9cd8adb..af9e477 100644 --- a/src/nt_funcs.rs +++ b/src/nt_funcs.rs @@ -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(factors: &BTreeMap) -> 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 diff --git a/src/primality.rs b/src/primality.rs index a8c96d4..4ec7da5 100644 --- a/src/primality.rs +++ b/src/primality.rs @@ -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); } diff --git a/src/tables.rs b/src/tables.rs index 5d7ca07..d5a552f 100644 --- a/src/tables.rs +++ b/src/tables.rs @@ -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,