Skip to content

Commit

Permalink
Add miller_rabin as selectable method to run in CLI (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
hesampakdaman authored Apr 9, 2024
1 parent e86828d commit c4b765b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bnum::types::U512;
use rustic_factors::algorithms;
use rustic_factors::primality_test;
use rustic_factors::traits::PrimalityTest;
use rustic_factors::Factorization;
use std::env;

Expand All @@ -19,12 +21,20 @@ fn run(args: Vec<String>) -> Result<(), String> {
.parse()
.map_err(|_| String::from("Please provide a valid positive integer"))?;
match method.as_str() {
"fermats_factorization_method" => println!("{}", Factorization::new::<algorithms::FermatsFactorizationMethod>(&n)),
"miller_rabin" => println!(
"is {} prime? {}",
&n,
primality_test::MillerRabin::is_prime(&n)
),
"fermats_factorization_method" => println!(
"{}",
Factorization::new::<algorithms::FermatsFactorizationMethod>(&n)
),
"pollards_rho" => println!("{}", Factorization::new::<algorithms::PollardsRho>(&n)),
"trial_division" => println!("{}", Factorization::new::<algorithms::TrialDivision>(&n)),
_ => {
return Err(String::from(
"Unknown algorithm. Available options: fermats_factorization_method, pollards_rho, trial_division",
"Unknown algorithm. Available options: fermats_factorization_method, miller_rabin, pollards_rho, trial_division",
));
}
};
Expand All @@ -37,7 +47,12 @@ mod tests {

#[test]
fn happy_cases() {
for method in ["pollards_rho", "trial_division"] {
for method in [
"fermats_factorization_method",
"miller_rabin",
"pollards_rho",
"trial_division",
] {
assert!(run(vec![
String::from("rustic_factors"),
String::from(method),
Expand All @@ -64,7 +79,7 @@ mod tests {
String::from("123")
])
.unwrap_err(),
String::from("Unknown algorithm. Available options: fermats_factorization_method, pollards_rho, trial_division")
String::from("Unknown algorithm. Available options: fermats_factorization_method, miller_rabin, pollards_rho, trial_division")
);
}
}

0 comments on commit c4b765b

Please sign in to comment.