mirror of
https://github.com/uutils/coreutils
synced 2024-11-17 18:28:18 +00:00
Merge pull request #1552 from nbraud/factor/faster/table
Speed up factor::table
This commit is contained in:
commit
ddd38403e6
2 changed files with 8 additions and 5 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue