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

68
problems/p37/src/main.rs Normal file
View File

@@ -0,0 +1,68 @@
use primal::{self, Primes, is_prime};
fn is_truncatable_primes(n: u32) -> bool {
match n {
2 => false,
3 => false,
5 => false,
7 => false,
_ => is_right_truncatable_primes(n) && is_left_truncatable_primes(n),
}
}
#[test]
fn test_is_truncatable_primes() {
assert!(is_truncatable_primes(3797));
}
fn is_left_truncatable_primes(n: u32) -> bool {
let mut n = n;
while n > 0 {
if is_prime(n as u64) {
n /= 10;
} else {
return false;
}
}
true
}
#[test]
fn test_is_left_truncatable_primes() {
assert!(is_left_truncatable_primes(3797));
}
fn is_right_truncatable_primes(n: u32) -> bool {
let mut n = n;
while n > 0 {
if is_prime(n as u64) {
let string = &n.to_string()[1..];
n = match string.len() {
0 => {
return true;
}
_ => string.parse().unwrap(),
}
} else {
return false;
}
}
true
}
#[test]
fn test_is_right_truncatable_primes() {
assert!(is_right_truncatable_primes(3797));
}
fn main() {
let mut p_vec = Vec::new();
for prime in Primes::all() {
if is_truncatable_primes(prime as u32) {
p_vec.push(prime as u32);
}
if p_vec.len() == 11 {
break;
}
}
let sum = p_vec.iter().sum::<u32>();
dbg!(p_vec);
println!("sum: {sum}");
}