[feature] single thread code to calculate hcns using prime factorization

This commit is contained in:
ddidderr 2023-12-07 18:38:36 +01:00
parent 2f6449a25a
commit 24cf088011
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -1,12 +1,56 @@
mod sieve;
use std::time::Instant;
use sieve::get_primes;
const MAX_SIEVED_PRIMES: usize = 100_000_000;
fn main() {
let primes = get_primes(MAX_SIEVED_PRIMES);
fn count_prime_factor_exponent(prime: u64, mut nr: u64) -> u64 {
let mut count = 0;
while nr % prime == 0 {
nr /= prime;
count += 1;
}
count
}
fn prime_factors(nr: u64, primes: &[u64]) -> u64 {
let mut num_teilers = 1;
let nr_sqrt = (nr as f64).sqrt().ceil() as u64;
for prime in primes {
println!("{prime}");
if *prime > nr_sqrt {
break;
}
let count = count_prime_factor_exponent(*prime, nr);
if count > 0 {
num_teilers *= count + 1;
}
}
num_teilers
}
fn main() {
let start = Instant::now();
println!("Precalculating primes...");
let primes = get_primes(MAX_SIEVED_PRIMES);
println!("{} primes. Took {:?}", primes.len(), start.elapsed());
let mut max_teilers = 0;
let start = Instant::now();
for nr in (2..1_000_000_000).step_by(2) {
let teilers = prime_factors(nr, &primes);
if teilers > max_teilers {
println!("{nr}: {teilers} ({:?} since start)", start.elapsed());
max_teilers = teilers;
}
}
}