init
This commit is contained in:
6
problems/p43/Cargo.toml
Normal file
6
problems/p43/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p43"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
40
problems/p43/src/main.rs
Normal file
40
problems/p43/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
fn solve() -> u64 {
|
||||
let mut digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
let mut sum = 0;
|
||||
loop {
|
||||
if digits[0] != 0 && check_property(&digits) {
|
||||
sum += digits.iter().fold(0u64, |acc, &d| acc * 10 + d as u64);
|
||||
}
|
||||
if !next_permutation(&mut digits) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
fn next_permutation<T: Ord>(array: &mut [T]) -> bool {
|
||||
let i = match array.windows(2).rposition(|w| w[0] < w[1]) {
|
||||
Some(i) => i,
|
||||
None => return false,
|
||||
};
|
||||
let j = array.iter().rposition(|x| x > &array[i]).unwrap();
|
||||
array.swap(i, j);
|
||||
array[i + 1..].reverse();
|
||||
true
|
||||
}
|
||||
|
||||
fn check_property(d: &[i32]) -> bool {
|
||||
let primes = [2, 3, 5, 7, 11, 13, 17];
|
||||
for i in 0..7 {
|
||||
let num = (d[i + 1] * 100 + d[i + 2] * 10 + d[i + 3]) as u64;
|
||||
if !num.is_multiple_of(primes[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
fn main() {
|
||||
let sum = solve();
|
||||
|
||||
println!("{sum}");
|
||||
}
|
||||
Reference in New Issue
Block a user