Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 293e9d2

Browse files
asder8215sylvestre
authored andcommitted
factor: use num_prime crate's u64 and u128 factorization methods to speed up the performance of calculating prime numbers for u64 and u128
1 parent 92bb655 commit 293e9d2

File tree

1 file changed

+83
-14
lines changed

1 file changed

+83
-14
lines changed

src/uu/factor/src/factor.rs

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,95 @@ fn print_factors_str(
3636
return Ok(());
3737
};
3838

39-
let (factorization, remaining) = if x > BigUint::from_u32(1).unwrap() {
40-
num_prime::nt_funcs::factors(x.clone(), None)
39+
if x > BigUint::from_u32(1).unwrap() {
40+
// use num_prime's factorize64 algorithm for u64 integers
41+
if x <= BigUint::from_u64(u64::MAX).unwrap() {
42+
let prime_factors = num_prime::nt_funcs::factorize64(x.clone().to_u64_digits()[0]);
43+
write_result_u64(w, &x, prime_factors, print_exponents)
44+
.map_err_context(|| translate!("factor-error-write-error"))?;
45+
}
46+
// use num_prime's factorize128 algorithm for u128 integers
47+
else if x <= BigUint::from_u128(u128::MAX).unwrap() {
48+
let rx = num_str.trim().parse::<u128>();
49+
let Ok(x) = rx else {
50+
// return Ok(). it's non-fatal and we should try the next number.
51+
show_warning!("{}: {}", num_str.maybe_quote(), rx.unwrap_err());
52+
set_exit_code(1);
53+
return Ok(());
54+
};
55+
let prime_factors = num_prime::nt_funcs::factorize128(x);
56+
write_result_u128(w, &x, prime_factors, print_exponents)
57+
.map_err_context(|| translate!("factor-error-write-error"))?;
58+
}
59+
// use num_prime's fallible factorization for anything greater than u128::MAX
60+
else {
61+
let (prime_factors, remaining) = num_prime::nt_funcs::factors(x.clone(), None);
62+
if let Some(_remaining) = remaining {
63+
return Err(USimpleError::new(
64+
1,
65+
translate!("factor-error-factorization-incomplete"),
66+
));
67+
}
68+
write_result_big_uint(w, &x, prime_factors, print_exponents)
69+
.map_err_context(|| translate!("factor-error-write-error"))?;
70+
}
4171
} else {
42-
(BTreeMap::new(), None)
43-
};
44-
45-
if let Some(_remaining) = remaining {
46-
return Err(USimpleError::new(
47-
1,
48-
translate!("factor-error-factorization-incomplete"),
49-
));
72+
let empty_primes: BTreeMap<BigUint, usize> = BTreeMap::new();
73+
write_result_big_uint(w, &x, empty_primes, print_exponents)
74+
.map_err_context(|| translate!("factor-error-write-error"))?;
5075
}
5176

52-
write_result(w, &x, factorization, print_exponents)
53-
.map_err_context(|| translate!("factor-error-write-error"))?;
54-
5577
Ok(())
5678
}
5779

58-
fn write_result(
80+
/// Writing out the prime factors for u64 integers
81+
fn write_result_u64(
82+
w: &mut io::BufWriter<impl Write>,
83+
x: &BigUint,
84+
factorization: BTreeMap<u64, usize>,
85+
print_exponents: bool,
86+
) -> io::Result<()> {
87+
write!(w, "{x}:")?;
88+
for (factor, n) in factorization {
89+
if print_exponents {
90+
if n > 1 {
91+
write!(w, " {factor}^{n}")?;
92+
} else {
93+
write!(w, " {factor}")?;
94+
}
95+
} else {
96+
w.write_all(format!(" {factor}").repeat(n).as_bytes())?;
97+
}
98+
}
99+
writeln!(w)?;
100+
w.flush()
101+
}
102+
103+
/// Writing out the prime factors for u128 integers
104+
fn write_result_u128(
105+
w: &mut io::BufWriter<impl Write>,
106+
x: &u128,
107+
factorization: BTreeMap<u128, usize>,
108+
print_exponents: bool,
109+
) -> io::Result<()> {
110+
write!(w, "{x}:")?;
111+
for (factor, n) in factorization {
112+
if print_exponents {
113+
if n > 1 {
114+
write!(w, " {factor}^{n}")?;
115+
} else {
116+
write!(w, " {factor}")?;
117+
}
118+
} else {
119+
w.write_all(format!(" {factor}").repeat(n).as_bytes())?;
120+
}
121+
}
122+
writeln!(w)?;
123+
w.flush()
124+
}
125+
126+
/// Writing out the prime factors for BigUint integers
127+
fn write_result_big_uint(
59128
w: &mut io::BufWriter<impl Write>,
60129
x: &BigUint,
61130
factorization: BTreeMap<BigUint, usize>,

0 commit comments

Comments
 (0)