From 74e9647bd90b11d19060c9f23493d87e4c07c510 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 4 Dec 2024 21:55:20 +0000 Subject: [PATCH] refactor: basically everything to be found --- src/bin/consecutive_prime_sum.rs | 6 ++---- src/bin/even_fibonacci_numbers.rs | 4 ++-- src/bin/factorial_digit_sum.rs | 4 ++-- src/bin/multiples_of_3_or_5.rs | 4 +--- src/bin/number_letter_counts.rs | 6 +++--- src/bin/pandigital_prime.rs | 2 +- src/bin/power_digit_sum.rs | 4 ++-- src/bin/prime_permutations.rs | 4 ++-- src/bin/special_pythagorean_triplet.rs | 8 +++----- 9 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/bin/consecutive_prime_sum.rs b/src/bin/consecutive_prime_sum.rs index 43b4977..1d65060 100644 --- a/src/bin/consecutive_prime_sum.rs +++ b/src/bin/consecutive_prime_sum.rs @@ -1,9 +1,7 @@ fn check_prime(x: u32) -> bool { let mut check = true; - for j in 2..=(x as u32).isqrt() { - if x % 2 == 0 { - check = false; - } else if x % j == 0 { + for j in 2..=x.isqrt() { + if x % j == 0 { check = false; } } diff --git a/src/bin/even_fibonacci_numbers.rs b/src/bin/even_fibonacci_numbers.rs index b13e6eb..7a33fec 100644 --- a/src/bin/even_fibonacci_numbers.rs +++ b/src/bin/even_fibonacci_numbers.rs @@ -2,8 +2,8 @@ fn main() { let mut sequence: Vec = vec![1, 2]; while sequence[sequence.len() - 1] <= 4000000 { let mut last = sequence[sequence.len() - 1]; - let mut secondlast = sequence[sequence.len() - 2]; - (secondlast, last) = (last, secondlast + last); + let secondlast = sequence[sequence.len() - 2]; + (_, last) = (last, secondlast+last); sequence.push(last); } for i in sequence.clone() { diff --git a/src/bin/factorial_digit_sum.rs b/src/bin/factorial_digit_sum.rs index a21f52c..530efea 100644 --- a/src/bin/factorial_digit_sum.rs +++ b/src/bin/factorial_digit_sum.rs @@ -1,11 +1,11 @@ use num_bigint::BigUint; fn factorial(x: u64) -> BigUint { - let mut product = BigUint::from(1 as u64); + let mut product = BigUint::from(1_u64); for i in 2..=x { product *= BigUint::from(i); } - product.into() + product } fn main() { diff --git a/src/bin/multiples_of_3_or_5.rs b/src/bin/multiples_of_3_or_5.rs index ea317d6..21526c5 100644 --- a/src/bin/multiples_of_3_or_5.rs +++ b/src/bin/multiples_of_3_or_5.rs @@ -1,9 +1,7 @@ fn main() { let mut multiples: Vec = vec![]; for num in 0..1000 { - if num % 3 == 0 { - multiples.push(num); - } else if num % 5 == 0 { + if num % 3 == 0 || num % 5 == 0 { multiples.push(num); } } diff --git a/src/bin/number_letter_counts.rs b/src/bin/number_letter_counts.rs index 1a5d5e0..f13da92 100644 --- a/src/bin/number_letter_counts.rs +++ b/src/bin/number_letter_counts.rs @@ -39,9 +39,9 @@ fn translate(number: u64) -> String { match number { number if number < 20 => { result = GENERIC[(number) as usize].to_string(); }, - number if number >= 20 && number < 100 => { + number if (20..100).contains(&number) => { let number_string = number.to_string(); - let tens = number_string.chars().nth(0).unwrap().to_digit(10).unwrap(); + let tens = number_string.chars().next().unwrap().to_digit(10).unwrap(); let ones = number_string.chars().nth(1).unwrap().to_digit(10).unwrap(); result = SPECIAL[tens as usize].to_owned()+GENERIC[(ones) as usize]; @@ -49,7 +49,7 @@ fn translate(number: u64) -> String { 100 => { result = "onehundred".to_string(); } number if number > 100 && number < 1000 => { let number_string = number.to_string(); - let hundreds = number_string.chars().nth(0).unwrap().to_digit(10).unwrap(); + let hundreds = number_string.chars().next().unwrap().to_digit(10).unwrap(); let tens = number_string.chars().nth(1).unwrap().to_digit(10).unwrap(); let ones = number_string.chars().nth(2).unwrap().to_digit(10).unwrap(); diff --git a/src/bin/pandigital_prime.rs b/src/bin/pandigital_prime.rs index 89e45b7..3f64362 100644 --- a/src/bin/pandigital_prime.rs +++ b/src/bin/pandigital_prime.rs @@ -17,7 +17,7 @@ fn is_prime(num: u64) -> bool { } } -fn permutations(input: &Vec) -> Vec { +fn permutations(input: &[u64]) -> Vec { let mut permutations = vec![]; for permutation in input.iter().permutations(input.len()).unique() { let result: u64 = permutation.iter().fold(0, |acc, &x| acc * 10u64.pow(x.to_string().len() as u32) + x); diff --git a/src/bin/power_digit_sum.rs b/src/bin/power_digit_sum.rs index 60e4620..17ad398 100644 --- a/src/bin/power_digit_sum.rs +++ b/src/bin/power_digit_sum.rs @@ -1,13 +1,13 @@ use num_bigint::BigUint; fn main() { - let mut number: BigUint = BigUint::from(2 as u32); + let mut number: BigUint = BigUint::from(2_u32); number = number.pow(1000); let number_list: Vec = number .to_string() .chars() - .filter_map(|c| c.to_digit(10).map(|d| d as u32)) + .filter_map(|c| c.to_digit(10)) .collect(); let sum: u32 = number_list.iter().sum(); diff --git a/src/bin/prime_permutations.rs b/src/bin/prime_permutations.rs index 63daf30..1300b64 100644 --- a/src/bin/prime_permutations.rs +++ b/src/bin/prime_permutations.rs @@ -17,7 +17,7 @@ fn is_prime(num: u64) -> bool { } } -fn permutations(input: &Vec) -> Vec { +fn permutations(input: &[u64]) -> Vec { let mut permutations = vec![]; for permutation in input.iter().permutations(input.len()).unique() { let result: u64 = permutation.iter().fold(0, |acc, &x| acc * 10u64.pow(x.to_string().len() as u32) + x); @@ -58,7 +58,7 @@ fn main() { for k in prime_permutations.clone().into_iter().filter(|x| *x != *j).collect::>() { if (*j as i64-i as i64).abs() == (k as i64-i as i64).abs() { - let difference = (*j as i64-i as i64).abs() as u64; + let difference = (*j as i64 - i as i64).unsigned_abs(); let final_vector: Vec = vec![i-difference, i, i+difference]; let concat = final_vector.into_iter().map(|s| s.to_string()).collect::(); diff --git a/src/bin/special_pythagorean_triplet.rs b/src/bin/special_pythagorean_triplet.rs index e71c330..0bf7593 100644 --- a/src/bin/special_pythagorean_triplet.rs +++ b/src/bin/special_pythagorean_triplet.rs @@ -4,11 +4,9 @@ fn main() { for j in 1..1000 { for k in 1..1000 { let pythagoras: f64 = (i as f64).powf(2.) + (j as f64).powf(2.); - if pythagoras == (k as f64).powf(2.) { - if i + j + k == 1000 { - println!("{}", i * j * k); - break 'algorithm; - } + if pythagoras == (k as f64).powf(2.) && i + j + k == 1000 { + println!("{}", i * j * k); + break 'algorithm; } } }