-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1558 from nbraud/factor/faster/centralise_logic
factor: Refactor the factoring logic
- Loading branch information
Showing
7 changed files
with
149 additions
and
121 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// * This file is part of the uutils coreutils package. | ||
// * | ||
// * (c) 2014 T. Jameson Little <t.jameson.little@gmail.com> | ||
// * (c) 2020 nicoo <nicoo@debian.org> | ||
// * | ||
// * For the full copyright and license information, please view the LICENSE file | ||
// * that was distributed with this source code. | ||
|
||
#[macro_use] | ||
extern crate uucore; | ||
|
||
use std::error::Error; | ||
use std::io::{self, stdin, stdout, BufRead, Write}; | ||
|
||
mod factor; | ||
pub(crate) use factor::*; | ||
|
||
mod miller_rabin; | ||
mod numeric; | ||
mod rho; | ||
mod table; | ||
|
||
static SYNTAX: &str = "[OPTION] [NUMBER]..."; | ||
static SUMMARY: &str = "Print the prime factors of the given number(s). | ||
If none are specified, read from standard input."; | ||
static LONG_HELP: &str = ""; | ||
|
||
fn print_factors_str(num_str: &str, w: &mut impl io::Write) -> Result<(), Box<dyn Error>> { | ||
num_str | ||
.parse::<u64>() | ||
.map_err(|e| e.into()) | ||
.and_then(|x| writeln!(w, "{}:{}", x, factor(x)).map_err(|e| e.into())) | ||
} | ||
|
||
pub fn uumain(args: impl uucore::Args) -> i32 { | ||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP).parse(args.collect_str()); | ||
let stdout = stdout(); | ||
let mut w = io::BufWriter::new(stdout.lock()); | ||
|
||
if matches.free.is_empty() { | ||
let stdin = stdin(); | ||
|
||
for line in stdin.lock().lines() { | ||
for number in line.unwrap().split_whitespace() { | ||
if let Err(e) = print_factors_str(number, &mut w) { | ||
show_warning!("{}: {}", number, e); | ||
} | ||
} | ||
} | ||
} else { | ||
for number in &matches.free { | ||
if let Err(e) = print_factors_str(number, &mut w) { | ||
show_warning!("{}: {}", number, e); | ||
} | ||
} | ||
} | ||
|
||
if let Err(e) = w.flush() { | ||
show_error!("{}", e); | ||
} | ||
|
||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.