mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +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;
|
return factors;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (factors, n) = table::factor(n, factors);
|
table::factor(&mut n, &mut factors);
|
||||||
|
|
||||||
#[allow(clippy::let_and_return)]
|
#[allow(clippy::let_and_return)]
|
||||||
let r = if n < (1 << 32) {
|
let r = if n < (1 << 32) {
|
||||||
|
|
|
@ -8,15 +8,13 @@
|
||||||
|
|
||||||
// spell-checker: ignore (ToDO) INVS
|
// spell-checker: ignore (ToDO) INVS
|
||||||
|
|
||||||
use std::num::Wrapping;
|
|
||||||
|
|
||||||
use crate::Factors;
|
use crate::Factors;
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/prime_table.rs"));
|
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 {
|
for &(prime, inv, ceil) in P_INVS_U64 {
|
||||||
if num == 1 {
|
if *num == 1 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,11 +25,11 @@ pub(crate) fn factor(mut num: u64, mut factors: Factors) -> (Factors, u64) {
|
||||||
// for a nice explanation.
|
// for a nice explanation.
|
||||||
let mut k = 0;
|
let mut k = 0;
|
||||||
loop {
|
loop {
|
||||||
let Wrapping(x) = Wrapping(num) * Wrapping(inv);
|
let x = num.wrapping_mul(inv);
|
||||||
|
|
||||||
// While prime divides num
|
// While prime divides num
|
||||||
if x <= ceil {
|
if x <= ceil {
|
||||||
num = x;
|
*num = x;
|
||||||
k += 1;
|
k += 1;
|
||||||
#[cfg(feature = "coz")]
|
#[cfg(feature = "coz")]
|
||||||
coz::progress!("factor found");
|
coz::progress!("factor found");
|
||||||
|
@ -43,6 +41,4 @@ pub(crate) fn factor(mut num: u64, mut factors: Factors) -> (Factors, u64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(factors, num)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue