1
0
This commit is contained in:
2026-02-04 11:15:03 +08:00
commit 8b20a5dd21
125 changed files with 4177 additions and 0 deletions

8
problems/p41/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "p41"
version = "0.1.0"
edition = "2024"
[dependencies]
primal = { workspace = true }
itertools = { workspace = true }

44
problems/p41/src/main.rs Normal file
View File

@@ -0,0 +1,44 @@
// 全数字素数
// 如果一个$n$位数恰好使用了$1$至$n$每个数字各一次,
// 则称为全数字数。例如,$2143$就是一个$4$位全数字数,
// 同时它也是一个素数。
//
// 最大的全数字素数是多少?
// 当n=1时只有1
// 当n=2时12和21都不是
// 当n>2时2不在最后一位。
// 当1+...+n 可以被3整除时一定不是
// 素数判定就用isprime函数
// 应该从大往小排序
use itertools::Itertools;
use primal::{self, is_prime};
fn digits_to_u32(digits: &[u8]) -> u32 {
let mut result = 0u32;
for &digit in digits {
result += digit as u32;
result *= 10;
}
result /= 10;
result
}
fn main() {
let num = [7, 6, 5, 4, 3, 2, 1];
let a = num
.iter()
.permutations(num.len())
.map(|x| {
let copied: Vec<u8> = x.iter().map(|&&d| d).collect();
digits_to_u32(&copied)
})
.collect::<Vec<u32>>();
for i in a {
if is_prime(i as u64) {
println!("{i}");
break;
}
}
}