质数检测
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);
}
}
当前内容版权归 rustlang-cn 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rustlang-cn .