mirror of
https://github.com/uutils/coreutils
synced 2024-11-17 02:08:09 +00:00
factor::table: Take mutable refs
This will be easier to adapt to working with multiple numbers to process at once.
This commit is contained in:
parent
6c830e2f25
commit
c68c83c6dd
2 changed files with 5 additions and 9 deletions
|
@ -161,7 +161,7 @@ pub fn factor(mut n: u64) -> Factors {
|
|||
return factors;
|
||||
}
|
||||
|
||||
let (factors, n) = table::factor(n, factors);
|
||||
table::factor(&mut n, &mut factors);
|
||||
|
||||
#[allow(clippy::let_and_return)]
|
||||
let r = if n < (1 << 32) {
|
||||
|
|
|
@ -8,15 +8,13 @@
|
|||
|
||||
// spell-checker: ignore (ToDO) INVS
|
||||
|
||||
use std::num::Wrapping;
|
||||
|
||||
use crate::Factors;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/prime_table.rs"));
|
||||
|
||||
pub(crate) fn factor(mut num: u64, mut factors: Factors) -> (Factors, u64) {
|
||||
pub(crate) fn factor(num: &mut u64, factors: &mut Factors) {
|
||||
for &(prime, inv, ceil) in P_INVS_U64 {
|
||||
if num == 1 {
|
||||
if *num == 1 {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -27,11 +25,11 @@ pub(crate) fn factor(mut num: u64, mut factors: Factors) -> (Factors, u64) {
|
|||
// for a nice explanation.
|
||||
let mut k = 0;
|
||||
loop {
|
||||
let Wrapping(x) = Wrapping(num) * Wrapping(inv);
|
||||
let x = num.wrapping_mul(inv);
|
||||
|
||||
// While prime divides num
|
||||
if x <= ceil {
|
||||
num = x;
|
||||
*num = x;
|
||||
k += 1;
|
||||
#[cfg(feature = "coz")]
|
||||
coz::progress!("factor found");
|
||||
|
@ -43,6 +41,4 @@ pub(crate) fn factor(mut num: u64, mut factors: Factors) -> (Factors, u64) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
(factors, num)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue