43
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "euler43"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.14.0"
|
||||
@@ -0,0 +1,45 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let numbers = generate_pandigital_numbers();
|
||||
|
||||
let mut sum = 0;
|
||||
for number in numbers {
|
||||
if satisfies_divisibility_rules(&number) {
|
||||
sum += number.parse::<usize>().unwrap();
|
||||
}
|
||||
}
|
||||
println!("{sum}");
|
||||
}
|
||||
|
||||
fn generate_pandigital_numbers() -> Vec<String> {
|
||||
('0'..='9')
|
||||
.permutations(10)
|
||||
.map(|c| c.iter().collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn satisfies_divisibility_rules(s: &str) -> bool {
|
||||
let n1: usize = extract(s, 1, 3);
|
||||
let n2: usize = extract(s, 2, 4);
|
||||
let n3: usize = extract(s, 3, 5);
|
||||
let n4: usize = extract(s, 4, 6);
|
||||
let n5: usize = extract(s, 5, 7);
|
||||
let n6: usize = extract(s, 6, 8);
|
||||
let n7: usize = extract(s, 7, 9);
|
||||
n1 % 2 == 0
|
||||
&& n2 % 3 == 0
|
||||
&& n3 % 5 == 0
|
||||
&& n4 % 7 == 0
|
||||
&& n5 % 11 == 0
|
||||
&& n6 % 13 == 0
|
||||
&& n7 % 17 == 0
|
||||
}
|
||||
|
||||
fn extract(s: &str, min: usize, max_inclusive: usize) -> usize {
|
||||
s.chars()
|
||||
.get(min..=max_inclusive)
|
||||
.collect::<String>()
|
||||
.parse()
|
||||
.unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user