Skip to content

Commit 9499d20

Browse files
committedMay 10, 2021
feat: better resolution
1 parent 42f5c5a commit 9499d20

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed
 

‎count_primes/src/lib.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,24 @@ impl Solution {
1818
// }
1919

2020
pub fn count_primes(n: i32) -> i32 {
21-
if n < 2 {
21+
if n <= 2 {
2222
return 0;
2323
}
24+
2425
let n = n as usize;
2526
let mut nums = vec![true; n];
2627
nums[0] = false;
2728
nums[1] = false;
28-
for i in 2..n {
29-
if i * i >= n {
30-
break;
31-
}
32-
if !nums[i] {
33-
continue;
34-
}
35-
for times in i..n {
36-
if times * i >= n {
37-
break;
29+
let mut i = 2;
30+
while i * i < n {
31+
if nums[i] {
32+
let mut j = 2;
33+
while i * j < n {
34+
nums[i * j] = false;
35+
j += 1;
3836
}
39-
nums[times * i] = false;
4037
}
38+
i += 1;
4139
}
4240
nums.iter().filter(|&&x| x).count() as i32
4341
}

0 commit comments

Comments
 (0)
Please sign in to comment.