Skip to content

Commit

Permalink
leetcode: Add 0204
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 27, 2024
1 parent b4b0763 commit 52edfda
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0204.count-primes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0204-count-primes"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0204.count-primes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
58 changes: 58 additions & 0 deletions src/leetcode/0204.count-primes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

// Enumeration
// 筛选法求质数
pub fn count_primes1(n: i32) -> i32 {
debug_assert!((0..=500_000).contains(&n));

if n <= 1 {
return 0;
}

let n = n as usize;
let mut primes: Vec<bool> = vec![true; n + 1];

//平方根
let mut prime_count: i32 = 0;
for i in 2..n {
// 如果这个数值是和数, 就直接跳过它.
if !primes[i] {
continue;
}
prime_count += 1;

let mut pos = i;
while pos < n {
primes[pos] = false;
pos += i;
}
}

prime_count
}

pub type SolutionFn = fn(i32) -> i32;

fn check_solution(func: SolutionFn) {
assert_eq!(func(10), 4);
assert_eq!(func(0), 0);
assert_eq!(func(1), 0);
assert_eq!(func(2), 0);
assert_eq!(func(3), 1);
}

fn main() {
check_solution(count_primes1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, count_primes1};

#[test]
fn test_count_primes1() {
check_solution(count_primes1);
}
}

0 comments on commit 52edfda

Please sign in to comment.