solutions: 41
This commit is contained in:
parent
8dadaeef8b
commit
f0858b63e6
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -8,6 +8,21 @@ version = "1.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
|
@ -40,5 +55,6 @@ dependencies = [
|
|||
name = "oily-solutions"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"num-bigint",
|
||||
]
|
||||
|
|
|
@ -4,4 +4,5 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.13.0"
|
||||
num-bigint = "0.4.6"
|
||||
|
|
46
src/bin/pandigital_prime.rs
Normal file
46
src/bin/pandigital_prime.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
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<u64>) -> Vec<u64> {
|
||||
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 pandigitals: Vec<u64> = vec![];
|
||||
let mut permutator: Vec<u64> = vec![];
|
||||
|
||||
for i in 1..=9 {
|
||||
permutator.push(i);
|
||||
|
||||
for j in permutations(&permutator) {
|
||||
if is_prime(j) {
|
||||
pandigitals.push(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", pandigitals.iter().max().unwrap());
|
||||
}
|
Loading…
Reference in a new issue