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

6
problems/p21/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "p21"
version = "0.1.0"
edition = "2024"
[dependencies]

33
problems/p21/src/main.rs Normal file
View File

@@ -0,0 +1,33 @@
fn d(n: u64) -> u64 {
let mut sum = 1u64;
let sqrt_n = (n as f64).sqrt() as u64;
for i in 2..=sqrt_n {
if n % i == 0 {
sum += i;
if i != n / i {
// 避免完全平方数重复计算
sum += n / i;
}
}
}
sum
}
fn main() {
let mut amicables: Vec<u64> = Vec::new();
for i in 2..=10000 {
if amicables.contains(&i) {
continue;
}
let pair = d(i);
if d(pair) == i && pair != i {
// 排除完美数
amicables.push(i);
amicables.push(pair);
}
}
let sum: u64 = amicables.iter().sum();
println!("{sum}");
}