1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

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(); 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 // By default, we print the multiplicative inverses mod 2^64 of the first 1k primes
const DEFAULT_SIZE: usize = 320;
let n = args() let n = args()
.nth(1) .nth(1)
.unwrap_or_else(|| "1027".to_string()) .and_then(|s| s.parse::<usize>().ok())
.parse::<usize>() .unwrap_or(DEFAULT_SIZE);
.ok()
.unwrap_or(1027);
write!(file, "{}", PREAMBLE).unwrap(); write!(file, "{}", PREAMBLE).unwrap();
let mut cols = 3; 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 // if (num * inv) mod 2^64 <= ceil, then prime divides num
// See https://math.stackexchange.com/questions/1251327/ // See https://math.stackexchange.com/questions/1251327/
// for a nice explanation. // for a nice explanation.
let mut k = 0;
loop { loop {
let Wrapping(x) = Wrapping(num) * Wrapping(inv); let Wrapping(x) = Wrapping(num) * Wrapping(inv);
// While prime divides num // While prime divides num
if x <= ceil { if x <= ceil {
num = x; num = x;
factors.push(prime); k += 1;
} else { } else {
if k > 0 {
factors.add(prime, k);
}
break; break;
} }
} }