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:
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();
|
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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue