Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ciphers/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ fn key_expansion(init_key: &[Byte], num_rounds: usize) -> Vec<Byte> {
}

fn add_round_key(data: &mut [Byte], round_key: &[Byte]) {
assert!(data.len() % AES_BLOCK_SIZE == 0 && round_key.len() == AES_BLOCK_SIZE);
assert!(data.len().is_multiple_of(AES_BLOCK_SIZE) && round_key.len() == AES_BLOCK_SIZE);
let num_blocks = data.len() / AES_BLOCK_SIZE;
data.iter_mut()
.zip(round_key.repeat(num_blocks))
Expand Down Expand Up @@ -348,7 +348,7 @@ fn mix_column_blocks(data: &mut [Byte], mode: AesMode) {
}

fn padding<T: Clone + Default>(data: &[T], block_size: usize) -> Vec<T> {
if data.len() % block_size == 0 {
if data.len().is_multiple_of(block_size) {
Vec::from(data)
} else {
let num_blocks = data.len() / block_size + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/ciphers/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn add(a: &mut Word, b: Word) {

#[inline]
const fn ceil(dividend: usize, divisor: usize) -> usize {
(dividend / divisor) + ((dividend % divisor != 0) as usize)
(dividend / divisor) + (!dividend.is_multiple_of(divisor) as usize)
}

fn g(v: &mut [Word; 16], a: usize, b: usize, c: usize, d: usize, x: Word, y: Word) {
Expand Down
5 changes: 1 addition & 4 deletions src/ciphers/diffie_hellman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ impl DiffieHellman {
// Both parties now have the same shared secret key s which can be used for encryption or authentication.

pub fn new(group: Option<u8>) -> Self {
let mut _group: u8 = 14;
if let Some(x) = group {
_group = x;
}
let _group = group.unwrap_or(14);

if !PRIMES.contains_key(&_group) {
panic!("group not in primes")
Expand Down
6 changes: 3 additions & 3 deletions src/ciphers/sha3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,10 @@ fn h2b(h: &[u8], n: usize) -> Vec<bool> {
}

fn b2h(s: &[bool]) -> Vec<u8> {
let m = if s.len() % U8BITS != 0 {
(s.len() / 8) + 1
} else {
let m = if s.len().is_multiple_of(U8BITS) {
s.len() / 8
} else {
(s.len() / 8) + 1
};
let mut bytes = vec![0u8; m];

Expand Down
2 changes: 1 addition & 1 deletion src/data_structures/probabilistic/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct MultiBinaryBloomFilter {

impl MultiBinaryBloomFilter {
pub fn with_dimensions(filter_size: usize, hash_count: usize) -> Self {
let bytes_count = filter_size / 8 + usize::from(filter_size % 8 > 0); // we need 8 times less entries in the array, since we are using bytes. Careful that we have at least one element though
let bytes_count = filter_size / 8 + usize::from(!filter_size.is_multiple_of(8)); // we need 8 times less entries in the array, since we are using bytes. Careful that we have at least one element though
Self {
filter_size,
bytes: vec![0; bytes_count],
Expand Down
2 changes: 1 addition & 1 deletion src/general/permutations/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn heap_recurse<T: Clone + Debug>(arr: &mut [T], k: usize, collector: &mut Vec<V
// Heap's algorithm has a more clever way of permuting the elements so that we never need to swap back!
for i in 0..k {
// now deal with [a, b]
let swap_idx = if k % 2 == 0 { i } else { 0 };
let swap_idx = if k.is_multiple_of(2) { i } else { 0 };
arr.swap(swap_idx, k - 1);
heap_recurse(arr, k - 1, collector);
}
Expand Down
2 changes: 1 addition & 1 deletion src/machine_learning/loss_function/kl_divergence_loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn kld_loss(actual: &[f64], predicted: &[f64]) -> f64 {
let loss: f64 = actual
.iter()
.zip(predicted.iter())
.map(|(&a, &p)| ((a + eps) * ((a + eps) / (p + eps)).ln()))
.map(|(&a, &p)| (a + eps) * ((a + eps) / (p + eps)).ln())
.sum();
loss
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/aliquot_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn aliquot_sum(number: u64) -> u64 {
panic!("Input has to be positive.")
}

(1..=number / 2).filter(|&d| number % d == 0).sum()
(1..=number / 2).filter(|&d| number.is_multiple_of(d)).sum()
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/math/collatz_sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn sequence(mut n: usize) -> Option<Vec<usize>> {
let mut list: Vec<usize> = vec![];
while n != 1 {
list.push(n);
if n % 2 == 0 {
if n.is_multiple_of(2) {
n /= 2;
} else {
n = 3 * n + 1;
Expand Down
2 changes: 1 addition & 1 deletion src/math/factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn factors(number: u64) -> Vec<u64> {
let mut factors: Vec<u64> = Vec::new();

for i in 1..=((number as f64).sqrt() as u64) {
if number % i == 0 {
if number.is_multiple_of(i) {
factors.push(i);
if i != number / i {
factors.push(number / i);
Expand Down
4 changes: 2 additions & 2 deletions src/math/interquartile_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn find_median(numbers: &[f64]) -> f64 {
let length = numbers.len();
let mid = length / 2;

if length % 2 == 0 {
if length.is_multiple_of(2) {
f64::midpoint(numbers[mid - 1], numbers[mid])
} else {
numbers[mid]
Expand All @@ -29,7 +29,7 @@ pub fn interquartile_range(numbers: &[f64]) -> f64 {

let length = numbers.len();
let mid = length / 2;
let (q1, q3) = if length % 2 == 0 {
let (q1, q3) = if length.is_multiple_of(2) {
let first_half = &numbers[0..mid];
let second_half = &numbers[mid..length];
(find_median(first_half), find_median(second_half))
Expand Down
2 changes: 1 addition & 1 deletion src/math/perfect_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub fn is_perfect_number(num: usize) -> bool {
let mut sum = 0;

for i in 1..num - 1 {
if num % i == 0 {
if num.is_multiple_of(i) {
sum += i;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/math/pollard_rho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn pollard_rho_get_one_factor(number: u64, seed: &mut u32, check_is_prime: b
fn get_small_factors(mut number: u64, primes: &[usize]) -> (u64, Vec<u64>) {
let mut result: Vec<u64> = Vec::new();
for p in primes {
while (number % *p as u64) == 0 {
while number.is_multiple_of(*p as u64) {
number /= *p as u64;
result.push(*p as u64);
}
Expand Down Expand Up @@ -201,7 +201,7 @@ mod test {
use super::*;

fn check_is_proper_factor(number: u64, factor: u64) -> bool {
factor > 1 && factor < number && ((number % factor) == 0)
factor > 1 && factor < number && number.is_multiple_of(factor)
}

fn check_factorization(number: u64, factors: &[u64]) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/math/prime_check.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub fn prime_check(num: usize) -> bool {
if (num > 1) & (num < 4) {
return true;
} else if (num < 2) || (num % 2 == 0) {
} else if (num < 2) || (num.is_multiple_of(2)) {
return false;
}

let stop: usize = (num as f64).sqrt() as usize + 1;
for i in (3..stop).step_by(2) {
if num % i == 0 {
if num.is_multiple_of(i) {
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/math/prime_factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub fn prime_factors(n: u64) -> Vec<u64> {
let mut n = n;
let mut factors = Vec::new();
while i * i <= n {
if n % i != 0 {
if n.is_multiple_of(i) {
n /= i;
factors.push(i);
} else {
if i != 2 {
i += 1;
}
i += 1;
} else {
n /= i;
factors.push(i);
}
}
if n > 1 {
Expand Down
2 changes: 1 addition & 1 deletion src/math/quadratic_residue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn is_residue(x: u64, modulus: u64) -> bool {
///
/// <https://en.wikipedia.org/wiki/Legendre_symbol>
pub fn legendre_symbol(a: u64, odd_prime: u64) -> i64 {
debug_assert!(odd_prime % 2 != 0, "prime must be odd");
debug_assert!(!odd_prime.is_multiple_of(2), "odd_prime must be odd");
if a == 0 {
0
} else if is_residue(a, odd_prime) {
Expand Down
4 changes: 2 additions & 2 deletions src/number_theory/euler_totient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub fn euler_totient(n: u64) -> u64 {
// Find all prime factors and apply formula
while p * p <= num {
// Check if p is a divisor of n
if num % p == 0 {
if num.is_multiple_of(p) {
// If yes, then it is a prime factor
// Apply the formula: result = result * (1 - 1/p)
while num % p == 0 {
while num.is_multiple_of(p) {
num /= p;
}
result -= result / p;
Expand Down