Skip to content

Commit

Permalink
use integer-sqrt
Browse files Browse the repository at this point in the history
  • Loading branch information
ognevny committed Oct 7, 2024
1 parent 1ad83c3 commit 98c6c3b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target
.vscode
.idea
*.lock
/target
/.vscode
/.idea
/*.lock
/.zed
# internal script
clean
/clean
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rust-version = "1.81.0"
[dependencies]
cfg-if = "1"
fastrand = { version = "2", default-features = false }
integer-sqrt = "0"
num-bigint = { version = "0", default-features = false }
rayon = { version = "1", optional = true }
regex = { version = "1", default-features = false }
Expand Down
9 changes: 6 additions & 3 deletions src/algorithm/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#[cfg(feature = "std")] use rayon::prelude::*;

use {crate::num::power::modpow, fastrand::Rng, num_bigint::BigUint, snafu::Snafu};
use {
crate::num::power::modpow, fastrand::Rng, integer_sqrt::IntegerSquareRoot, num_bigint::BigUint,
snafu::Snafu,
};

/// If number is less than 2, we can't say that number is either prime or composite.
#[non_exhaustive]
Expand Down Expand Up @@ -142,8 +145,8 @@ pub fn sqrtest(num: usize) -> Result<PrimeStatus, PrimeStatusError> {
Err(PrimeStatusError)
} else {
// FIXME: https://github.com/rust-lang/rust/issues/116226
// let sqrt_res = num.isqrt().unsigned_abs()
let sqrt_res = (num as f64).sqrt().ceil() as usize;
// let sqrt_res = num.isqrt() + 1;
let sqrt_res = num.integer_sqrt() + 1;
if (3..=sqrt_res).into_par_iter().find_any(|&i| num % i == 0).is_some() {
Ok(PrimeStatus::Composite)
} else {
Expand Down

0 comments on commit 98c6c3b

Please sign in to comment.