From 0c795cbd69e561064f83aa344b93ac333f2d2803 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 4 Dec 2024 21:49:25 +0000 Subject: [PATCH] solutions: 49 --- src/bin/prime_permutations.rs | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/bin/prime_permutations.rs diff --git a/src/bin/prime_permutations.rs b/src/bin/prime_permutations.rs new file mode 100644 index 0000000..63daf30 --- /dev/null +++ b/src/bin/prime_permutations.rs @@ -0,0 +1,77 @@ +use itertools::Itertools; + +fn is_prime(num: u64) -> bool { + let prime: bool = true; + match num { + 0..=1 => false, + 2 => true, + num if num % 2 == 0 => false, + _ => { + for i in (3..=((num as f64).sqrt() as u64)).step_by(2) { + if num % i == 0 { + return false; + } + } + prime + } + } +} + +fn permutations(input: &Vec) -> 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); + permutations.push(result); + } + + permutations +} + +fn main() { + let mut primes: Vec = vec![]; + + for i in 1000..=10000 { + if is_prime(i) { + primes.push(i); + } + } + + let mut values: Vec = vec![]; + + for i in primes { + let mut prime_permutations: Vec = vec![]; + + let i_vector: Vec = i.to_string().chars().filter_map(|c| c.to_digit(10).map(|d| d as u64)).collect(); + for j in permutations(&i_vector) { + if is_prime(j) { + prime_permutations.push(j); + } + } + + prime_permutations.sort(); + let prime_permutations: Vec = prime_permutations.into_iter().filter(|x| *x != i).collect(); + + for j in &prime_permutations { + if *j < 1000 { + break; + } + + 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 final_vector: Vec = vec![i-difference, i, i+difference]; + + let concat = final_vector.into_iter().map(|s| s.to_string()).collect::(); + + if !values.contains(&concat) { + values.push(concat); + } + } + } + } + } + + for i in &values { + println!("{}", i); + } +}