mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
21 lines
306 B
Go
21 lines
306 B
Go
package leetcode
|
|
|
|
func countPrimes(n int) int {
|
|
isNotPrime := make([]bool, n)
|
|
for i := 2; i*i < n; i++ {
|
|
if isNotPrime[i] {
|
|
continue
|
|
}
|
|
for j := i * i; j < n; j = j + i {
|
|
isNotPrime[j] = true
|
|
}
|
|
}
|
|
count := 0
|
|
for i := 2; i < n; i++ {
|
|
if !isNotPrime[i] {
|
|
count++
|
|
}
|
|
}
|
|
return count
|
|
}
|