forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime_check.rs
33 lines (29 loc) · 768 Bytes
/
prime_check.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pub fn prime_check(num: usize) -> bool {
if (num > 1) & (num < 4) {
return true;
} else if (num < 2) || (num % 2 == 0) {
return false;
}
let stop: usize = (num as f64).sqrt() as usize + 1;
for i in (3..stop).step_by(2) {
if num % i == 0 {
return false;
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic() {
assert_eq!(prime_check(3), true);
assert_eq!(prime_check(7), true);
assert_eq!(prime_check(11), true);
assert_eq!(prime_check(2003), true);
assert_eq!(prime_check(4), false);
assert_eq!(prime_check(6), false);
assert_eq!(prime_check(21), false);
assert_eq!(prime_check(2004), false);
}
}