Simplify by eliminating sieve
This commit is contained in:
parent
e379adfc21
commit
664ea1528a
2 changed files with 0 additions and 19 deletions
|
@ -2,6 +2,3 @@
|
|||
edition = "2021"
|
||||
name = "prime_factors"
|
||||
version = "1.1.0"
|
||||
|
||||
[dependencies]
|
||||
bit-set = "0.8.0"
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue