Merge pull request #1552 from nbraud/factor/faster/table

Speed up factor::table
This commit is contained in:
Alex Lyon 2020-06-20 13:55:14 -07:00 committed by GitHub
commit ddd38403e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View file

@ -39,12 +39,11 @@ fn main() {
let mut file = File::create(&Path::new(&out_dir).join("prime_table.rs")).unwrap();
// By default, we print the multiplicative inverses mod 2^64 of the first 1k primes
const DEFAULT_SIZE: usize = 320;
let n = args()
.nth(1)
.unwrap_or_else(|| "1027".to_string())
.parse::<usize>()
.ok()
.unwrap_or(1027);
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(DEFAULT_SIZE);
write!(file, "{}", PREAMBLE).unwrap();
let mut cols = 3;

View file

@ -26,14 +26,18 @@ pub(crate) fn factor(mut num: u64) -> (Factors, u64) {
// if (num * inv) mod 2^64 <= ceil, then prime divides num
// See https://math.stackexchange.com/questions/1251327/
// for a nice explanation.
let mut k = 0;
loop {
let Wrapping(x) = Wrapping(num) * Wrapping(inv);
// While prime divides num
if x <= ceil {
num = x;
factors.push(prime);
k += 1;
} else {
if k > 0 {
factors.add(prime, k);
}
break;
}
}