Skip to content

Commit

Permalink
Fix negative number hanging error
Browse files Browse the repository at this point in the history
  • Loading branch information
callumjamesfortune committed Apr 26, 2024
1 parent 7bf9520 commit 9b4c0e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/miller_rabin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ use num_integer::Integer;
use rand::thread_rng;

pub fn miller_rabin(candidate: &BigInt) -> bool {

if candidate < &BigInt::from(0) {
return false;
}

if candidate.is_even() {
return *candidate == BigInt::from(2); // 2 is prime
}
Expand Down
14 changes: 14 additions & 0 deletions tests/miller_rabin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ mod tests {
use super::*;
use num_bigint::BigInt;

#[test]
fn test_negative_numbers() {

let primes = vec![
BigInt::from(-150),
BigInt::from(-3),
BigInt::from(0),
BigInt::from(-1005050)
];
for prime in primes {
assert_eq!(miller_rabin(&prime), false);
}
}

#[test]
fn test_primes() {

Expand Down

0 comments on commit 9b4c0e9

Please sign in to comment.