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

7
problems/p23/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "p23"
version = "0.1.0"
edition = "2024"
[dependencies]
rayon = "1.11.0"

61
problems/p23/src/main.rs Normal file
View File

@@ -0,0 +1,61 @@
// 小于28123的数分为可用两盈数和表示和不可表示。
// 计算范围在0,28123
// 找出范围内的盈数,两两加和
// 去除范围内的加和数,对剩余数求和
use rayon::prelude::*;
fn get_proper_divisors(num: u32) -> Vec<u32> {
if num <= 1 {
return Vec::new();
}
let mut divisors = Vec::new();
for i in 1..=((num as f64).sqrt() as u32) {
if num % i == 0 {
divisors.push(i);
if i != num / i && i != 1 {
// 避免重复且不包含1的平方根情况
divisors.push(num / i);
}
}
}
// 移除数字本身,只保留真因数
divisors.retain(|&x| x < num);
divisors.sort();
divisors
}
#[test]
fn test_get_proper_divisors() {
assert_eq!(vec![1, 2, 4, 7, 14], get_proper_divisors(28));
}
fn is_abundant(num: u32) -> bool {
let proper_sum: u32 = get_proper_divisors(num).iter().sum();
proper_sum > num
}
#[test]
fn test_is_abundant() {
assert!(is_abundant(12));
assert!(!is_abundant(4));
}
fn main() {
let all_abundant: Vec<u32> = (12..28123)
.into_par_iter()
.filter(|&x| is_abundant(x))
.collect();
let mut all_abundant_pair_sum = Vec::new();
for (k, adder1) in all_abundant.iter().enumerate() {
for adder2 in &all_abundant[k..] {
all_abundant_pair_sum.push(adder1 + adder2);
}
}
let result = (1..28123)
.into_par_iter()
.filter(|x| !all_abundant_pair_sum.contains(x))
.sum::<u32>();
println!("{result}");
}