refactor: basically everything to be found

This commit is contained in:
Muhammad Nauman Raza 2024-12-04 21:55:20 +00:00
parent 0c795cbd69
commit 74e9647bd9
Signed by: devraza
GPG key ID: 91EAD6081011574B
9 changed files with 18 additions and 24 deletions

View file

@ -1,9 +1,7 @@
fn check_prime(x: u32) -> bool { fn check_prime(x: u32) -> bool {
let mut check = true; let mut check = true;
for j in 2..=(x as u32).isqrt() { for j in 2..=x.isqrt() {
if x % 2 == 0 { if x % j == 0 {
check = false;
} else if x % j == 0 {
check = false; check = false;
} }
} }

View file

@ -2,8 +2,8 @@ fn main() {
let mut sequence: Vec<i32> = vec![1, 2]; let mut sequence: Vec<i32> = vec![1, 2];
while sequence[sequence.len() - 1] <= 4000000 { while sequence[sequence.len() - 1] <= 4000000 {
let mut last = sequence[sequence.len() - 1]; let mut last = sequence[sequence.len() - 1];
let mut secondlast = sequence[sequence.len() - 2]; let secondlast = sequence[sequence.len() - 2];
(secondlast, last) = (last, secondlast + last); (_, last) = (last, secondlast+last);
sequence.push(last); sequence.push(last);
} }
for i in sequence.clone() { for i in sequence.clone() {

View file

@ -1,11 +1,11 @@
use num_bigint::BigUint; use num_bigint::BigUint;
fn factorial(x: u64) -> 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 { for i in 2..=x {
product *= BigUint::from(i); product *= BigUint::from(i);
} }
product.into() product
} }
fn main() { fn main() {

View file

@ -1,9 +1,7 @@
fn main() { fn main() {
let mut multiples: Vec<i32> = vec![]; let mut multiples: Vec<i32> = vec![];
for num in 0..1000 { for num in 0..1000 {
if num % 3 == 0 { if num % 3 == 0 || num % 5 == 0 {
multiples.push(num);
} else if num % 5 == 0 {
multiples.push(num); multiples.push(num);
} }
} }

View file

@ -39,9 +39,9 @@ fn translate(number: u64) -> String {
match number { match number {
number if number < 20 => { result = GENERIC[(number) as usize].to_string(); }, 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 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(); let ones = number_string.chars().nth(1).unwrap().to_digit(10).unwrap();
result = SPECIAL[tens as usize].to_owned()+GENERIC[(ones) as usize]; 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(); } 100 => { result = "onehundred".to_string(); }
number if number > 100 && number < 1000 => { number if number > 100 && number < 1000 => {
let number_string = number.to_string(); 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 tens = number_string.chars().nth(1).unwrap().to_digit(10).unwrap();
let ones = number_string.chars().nth(2).unwrap().to_digit(10).unwrap(); let ones = number_string.chars().nth(2).unwrap().to_digit(10).unwrap();

View file

@ -17,7 +17,7 @@ fn is_prime(num: u64) -> bool {
} }
} }
fn permutations(input: &Vec<u64>) -> Vec<u64> { fn permutations(input: &[u64]) -> Vec<u64> {
let mut permutations = vec![]; let mut permutations = vec![];
for permutation in input.iter().permutations(input.len()).unique() { 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); let result: u64 = permutation.iter().fold(0, |acc, &x| acc * 10u64.pow(x.to_string().len() as u32) + x);

View file

@ -1,13 +1,13 @@
use num_bigint::BigUint; use num_bigint::BigUint;
fn main() { fn main() {
let mut number: BigUint = BigUint::from(2 as u32); let mut number: BigUint = BigUint::from(2_u32);
number = number.pow(1000); number = number.pow(1000);
let number_list: Vec<u32> = number let number_list: Vec<u32> = number
.to_string() .to_string()
.chars() .chars()
.filter_map(|c| c.to_digit(10).map(|d| d as u32)) .filter_map(|c| c.to_digit(10))
.collect(); .collect();
let sum: u32 = number_list.iter().sum(); let sum: u32 = number_list.iter().sum();

View file

@ -17,7 +17,7 @@ fn is_prime(num: u64) -> bool {
} }
} }
fn permutations(input: &Vec<u64>) -> Vec<u64> { fn permutations(input: &[u64]) -> Vec<u64> {
let mut permutations = vec![]; let mut permutations = vec![];
for permutation in input.iter().permutations(input.len()).unique() { 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); 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::<Vec<u64>>() { for k in prime_permutations.clone().into_iter().filter(|x| *x != *j).collect::<Vec<u64>>() {
if (*j as i64-i as i64).abs() == (k as i64-i as i64).abs() { 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<u64> = vec![i-difference, i, i+difference]; let final_vector: Vec<u64> = vec![i-difference, i, i+difference];
let concat = final_vector.into_iter().map(|s| s.to_string()).collect::<String>(); let concat = final_vector.into_iter().map(|s| s.to_string()).collect::<String>();

View file

@ -4,11 +4,9 @@ fn main() {
for j in 1..1000 { for j in 1..1000 {
for k in 1..1000 { for k in 1..1000 {
let pythagoras: f64 = (i as f64).powf(2.) + (j as f64).powf(2.); let pythagoras: f64 = (i as f64).powf(2.) + (j as f64).powf(2.);
if pythagoras == (k as f64).powf(2.) { if pythagoras == (k as f64).powf(2.) && i + j + k == 1000 {
if i + j + k == 1000 { println!("{}", i * j * k);
println!("{}", i * j * k); break 'algorithm;
break 'algorithm;
}
} }
} }
} }