Debian Codegraph @

debian/rust-vendor/num-prime/examples/divisor_sigma.rs

raw
1 use num_prime::nt_funcs::factorize64;
2
3 /// Return all divisors of the target
4 fn divisors(target: u64) -> Vec<u64> {
5 let factors = factorize64(target);
6 let mut result = Vec::with_capacity(factors.values().map(|e| e + 1).product());
7 result.push(1);
8
9 for (p, e) in factors {
10 // the new results contain all previous divisors multiplied by p, p^2, .., p^e
11 let mut new_result = Vec::with_capacity(result.len() * e);
12 for i in 1..=(e as u32) {
13 new_result.extend(result.iter().map(|f| f * p.pow(i)));
14 }
15 result.append(&mut new_result);
16 }
17 result
18 }
19
20 /// Calculate the divisor sigma function `σ_z(n`) on the target
21 /// Reference: <https://en.wikipedia.org/wiki/Divisor_function>
22 fn divisor_sigma(target: u64, z: u32) -> u64 {
23 divisors(target).into_iter().map(|d| d.pow(z)).sum()
24 }
25
26 fn main() {
27 println!("Divisor sigma with z=1 of numbers from 10 to 99:");
28 for i in 10..100 {
29 println!("{}: {:?}", i, divisor_sigma(i, 1));
30 }
31 }