Simplify by eliminating sieve

This commit is contained in:
Avery Winters 2024-11-06 14:47:44 -06:00
parent e379adfc21
commit 664ea1528a
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI
2 changed files with 0 additions and 19 deletions

View file

@ -2,6 +2,3 @@
edition = "2021"
name = "prime_factors"
version = "1.1.0"
[dependencies]
bit-set = "0.8.0"

View file

@ -1,17 +1,9 @@
use ::bit_set::BitSet;
pub fn factors(n: u64) -> Vec<u64> {
let n = n
.try_into()
.expect("usize is not large enough to factor a u64");
let bound = isqrt(n);
let mut not_primes = {
// We need to be able to store any integer up to and including the bound.
let capacity = bound + 1;
BitSet::with_capacity(capacity)
};
let mut factors = {
// Worst case number of factors is a power of 2, number of
// factors will be the number of bits needed to represent
@ -22,9 +14,6 @@ pub fn factors(n: u64) -> Vec<u64> {
let mut reduced = n;
'outer: for i in 2..=bound {
if not_primes.contains(i) {
continue;
}
while reduced % i == 0 {
{
let i = i
@ -39,11 +28,6 @@ pub fn factors(n: u64) -> Vec<u64> {
break 'outer;
}
}
for j in (i * i..=bound).step_by(i) {
// Ensure we allocated enough capacity for all non-primes.
assert!(j < not_primes.capacity());
not_primes.insert(j);
}
}
if reduced > 1 {