From dfc52a1c48e32b822f0ad96d0d9ef6348249ce90 Mon Sep 17 00:00:00 2001 From: Watagon Date: Wed, 16 Sep 2026 10:58:06 +0900 Subject: [PATCH 1/2] fix: clippy error --- src/ciphers/diffie_hellman.rs | 6 +----- src/dynamic_programming/fibonacci.rs | 2 +- src/math/miller_rabin.rs | 8 ++++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ciphers/diffie_hellman.rs b/src/ciphers/diffie_hellman.rs index 86b2c9d19b1..d749b3c4081 100644 --- a/src/ciphers/diffie_hellman.rs +++ b/src/ciphers/diffie_hellman.rs @@ -262,7 +262,7 @@ impl DiffieHellman { .unwrap_or_else(|_| BigUint::parse_bytes(b"0", 16).unwrap()); // Check if the other public key is valid based on NIST SP800-56 - if BigUint::from(2_u8) <= key + BigUint::from(2_u8) <= key && key <= &self.prime - BigUint::from(2_u8) && !key .modpow( @@ -270,10 +270,6 @@ impl DiffieHellman { &self.prime, ) .is_zero() - { - return true; - } - false } /// Generate the shared key diff --git a/src/dynamic_programming/fibonacci.rs b/src/dynamic_programming/fibonacci.rs index f1a55ce77f1..cf5fbbafeea 100644 --- a/src/dynamic_programming/fibonacci.rs +++ b/src/dynamic_programming/fibonacci.rs @@ -193,7 +193,7 @@ pub fn binary_lifting_fibonacci(n: u32) -> u128 { // the state always stores F(k), F(k+1) for some k, initially F(0), F(1) let mut state = (0u128, 1u128); - for i in (0..u32::BITS - n.leading_zeros()).rev() { + for i in (0..n.bit_width()).rev() { // compute F(2k), F(2k+1) from F(k), F(k+1) state = ( state.0 * (2 * state.1 - state.0), diff --git a/src/math/miller_rabin.rs b/src/math/miller_rabin.rs index dbeeac5acbd..51d365f268d 100644 --- a/src/math/miller_rabin.rs +++ b/src/math/miller_rabin.rs @@ -68,13 +68,13 @@ pub fn big_miller_rabin(number_ref: &BigUint, bases: &[u64]) -> u64 { let number = number_ref.clone(); if BigUint::from(5u32).cmp(&number) == Ordering::Greater { - if number.eq(&BigUint::zero()) { + return if number.eq(&BigUint::zero()) { panic!("0 is invalid input for Miller-Rabin. 0 is not prime by definition, but has no witness"); } else if number.eq(&BigUint::from(2u32)) || number.eq(&BigUint::from(3u32)) { - return 0; + 0 } else { - return number.to_u64().unwrap(); - } + number.to_u64().unwrap() + }; } if let Some(num) = number.to_u64() { From 8093a27d96291224f07ba112e9c7efcb32dd80d2 Mon Sep 17 00:00:00 2001 From: Watagon Date: Wed, 16 Sep 2026 10:00:35 +0900 Subject: [PATCH 2/2] refactor: simplify kth_factor implementation using iterator methods --- src/number_theory/kth_factor.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/number_theory/kth_factor.rs b/src/number_theory/kth_factor.rs index abfece86bb9..7e7d6c2ff75 100644 --- a/src/number_theory/kth_factor.rs +++ b/src/number_theory/kth_factor.rs @@ -2,17 +2,10 @@ // The idea is to check for each number in the range [N, 1], and print the Kth number that divides N completely. pub fn kth_factor(n: i32, k: i32) -> i32 { - let mut factors: Vec = Vec::new(); - let k = (k as usize) - 1; - for i in 1..=n { - if n % i == 0 { - factors.push(i); - } - if let Some(number) = factors.get(k) { - return *number; - } - } - -1 + (1..=n) + .filter(|&i| n % i == 0) + .nth(k as usize - 1) + .unwrap_or(-1) } #[cfg(test)]