Skip to content

Commit

Permalink
factors automatically uses u64 variant...
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Mar 24, 2024
1 parent 4167ba8 commit 43c6a91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
26 changes: 16 additions & 10 deletions src/uu/factor/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use clap::{crate_version, Arg, ArgAction, Command};
use num_bigint::BigUint;
use num_traits::FromPrimitive;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, FromIo, UResult};
use uucore::error::{set_exit_code, UResult, USimpleError};
use uucore::{format_usage, help_about, help_usage, show_error, show_warning};

const ABOUT: &str = help_about!("factor.md");
Expand All @@ -29,7 +29,7 @@ fn print_factors_str(
num_str: &str,
w: &mut io::BufWriter<impl io::Write>,
print_exponents: bool,
) -> io::Result<()> {
) -> UResult<()> {
let x = match num_str.trim().parse::<num_bigint::BigUint>() {
Ok(x) => x,
Err(e) => {
Expand All @@ -43,12 +43,19 @@ fn print_factors_str(

write!(w, "{x}:")?;

let factorization = if x > BigUint::from_u32(1).unwrap() {
num_prime::nt_funcs::factorize(x.clone())
let (factorization, remaining) = if x > BigUint::from_u32(1).unwrap() {
num_prime::nt_funcs::factors(x.clone(), None)
} else {
BTreeMap::new()
(BTreeMap::new(), None)
};

if let Some(_remaining) = remaining {
return Err(USimpleError::new(

Check warning on line 53 in src/uu/factor/src/cli.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/factor/src/cli.rs#L53

Added line #L53 was not covered by tests
1,
"Factorization incomplete. Remainders exists.",
));
}

Check warning on line 57 in src/uu/factor/src/cli.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/factor/src/cli.rs#L57

Added line #L57 was not covered by tests

// If print_exponents is true, use the alternate format specifier {:#} from fmt to print the factors
// of x in the form of p^e.
for (factor, n) in factorization {
Expand All @@ -65,7 +72,8 @@ fn print_factors_str(
}
}
writeln!(w)?;
w.flush()
w.flush()?;
Ok(())
}

#[uucore::main]
Expand All @@ -81,8 +89,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

if let Some(values) = matches.get_many::<String>(options::NUMBER) {
for number in values {
print_factors_str(number, &mut w, print_exponents)
.map_err_context(|| "write error".into())?;
print_factors_str(number, &mut w, print_exponents)?;
}
} else {
let stdin = stdin();
Expand All @@ -91,8 +98,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
match line {
Ok(line) => {
for number in line.split_whitespace() {
print_factors_str(number, &mut w, print_exponents)
.map_err_context(|| "write error".into())?;
print_factors_str(number, &mut w, print_exponents)?;
}
}
Err(e) => {
Expand Down
14 changes: 5 additions & 9 deletions tests/benches/factor/benches/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore funcs

use array_init::array_init;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use std::{collections::BTreeMap, mem};
use num_bigint::BigUint;
use num_traits::FromPrimitive;


fn table(c: &mut Criterion) {
#[cfg(target_os = "linux")]
Expand All @@ -31,11 +30,8 @@ fn table(c: &mut Criterion) {
let a_str = format!("{:?}", a);
group.bench_with_input(BenchmarkId::new("factor", &a_str), &a, |b, &a| {
b.iter(|| {
let mut n_s = a;
let mut f_s: [BTreeMap<BigUint, usize>; INPUT_SIZE] = array_init(|_| BTreeMap::new());
for (&mut n, f) in n_s.iter_mut().zip(f_s.iter_mut()) {
let r: BTreeMap<BigUint, usize> = num_prime::nt_funcs::factorize(BigUint::from_u64(n).unwrap());
_ = mem::replace(f, r);
for n in a {
let _r = num_prime::nt_funcs::factors(n, None);
}
});
});
Expand Down

0 comments on commit 43c6a91

Please sign in to comment.