Description:

Count the number of prime numbers less than a non-negative number, n

click to show more hints.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

计数出小于非负整数n的质数数量。质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。

解法:埃拉托斯特尼筛法 Sieve of Eratosthenes

如果一个数是另一个数的倍数,那这个数肯定不是质数。利用这个性质,可以建立一个质数数组,从2开始将素数的倍数都标注为不是质数。第一轮将4、6、8等表为非质数,然后遍历到3,发现3没有被标记为非质数,则将6、9、12等标记为非质数,一直到N为止,再数一遍质数数组中有多少质数。

Java:

public class Solution {
public int countPrimes(int n) {
boolean[] prime = new boolean[n];
Arrays.fill(prime, true);
for(int i = 2; i < n; i++){
if(prime[i]){
// 将i的2倍、3倍、4倍...都标记为非素数
for(int j = i * 2; j < n; j = j + i){
prime[j] = false;
}
}
}
int count = 0;
for(int i = 2; i < n; i++){
if(prime[i]) count++;
}
return count;
}
}

Python:

class Solution:
# @param {integer} n
# @return {integer}
def countPrimes(self, n):
isPrime = [True] * max(n, 2)
isPrime[0], isPrime[1] = False, False
x = 2
while x * x < n:
if isPrime[x]:
p = x * x
while p < n:
isPrime[p] = False
p += x
x += 1
return sum(isPrime)

Python:

class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 2: return 0
vis = [False] * n
for i in range(2, int(n ** 0.5) + 1):
if vis[i]: continue
j = i
while j * i < n:
vis[j * i] = True
j += 1
ans = 0
for i in range(2, n):
if not vis[i]: ans += 1
return ans

C++:

class Solution {
public:
int countPrimes(int n) {
if(!n||n==1) return 0;
vector<bool> isPrime(n,true);
// Loop's ending condition is i * i < n instead of i < sqrt(n)
// to avoid repeatedly calling an expensive function sqrt().
for(int i=2;i*i<n;++i)
{
if(!isPrime[i]) continue;
//填表起点i*i,如3*3,因为3*2已填,步长+i
for(int j=i*i;j<n;j+=i)
{
isPrime[j]=false;
}
}
int count=0;
for(int i=2;i<n;++i)
{
if(isPrime[i]) ++count;
}
return count;
}
};

  

All LeetCode Questions List 题目汇总

  

[LeetCode] 204. Count Primes 计数质数的更多相关文章

  1. LeetCode 204. Count Primes计数质数 (C++)

    题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...

  2. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  3. 204 Count Primes 计数质数

    计算所有小于非负整数 n 的质数数量. 详见:https://leetcode.com/problems/count-primes/description/ Java实现: 埃拉托斯特尼筛法:从2开始 ...

  4. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  5. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  8. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  9. Leetcode 204 Count Primes 数论

    题意:统计小于n的质数个数. 作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法. 是的,素数筛法并不是该题最佳的解法,线性素数筛法才是. 至于什么是素数筛法,请百度吧. class Sol ...

随机推荐

  1. 性能:Receiver层面

    创建多个接收器 多个端口启动多个receiver在其他Executor,接收多个端口数据,在吞吐量上提高其性能.代码上: import org.apache.spark.storage.Storage ...

  2. MyBatis框架的insert节点-向数据库中插入数据

    需求:使用mybatis框架中的insert元素节点向数据库中插入数据 UserMapper.xml UserMapper.java 编写测试方法: @Test public void testAdd ...

  3. 7-html列表

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  4. reshape()函数

    """ 1.当原始数组A[4,6]为二维数组,代表4行6列. A.reshape(-1,8):表示将数组转换成8列的数组,具体多少行我们不知道,所以参数设为-1.用我们的 ...

  5. [Codeforces 1265E]Beautiful Mirrors

    Description 题库链接 一共有 \(n\) 个关卡,你初始在第一个关卡.通过第 \(i\) 个关卡的概率为 \(p_i\).每一轮你可以挑战一个关卡.若通过第 \(i\) 个关卡,则进入第 ...

  6. 域权限维持:Ptk(Pass The Ticket)

    Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据

  7. SpringBoot学习(五)RSocket和Security

    一.RSocket RSocket是一个用于字节流传输的二进制协议.它通过在单个连接上传递异步消息来支持对称交互模型,主要支持的通讯层包括 TCP, WebSockets和Aeron(UDP). RS ...

  8. solidworks 学习 (四)

    旋钮三维建模

  9. 【JOISC2018|2019】【20190622】minerals

    题目 交互题 有\(2n\)个物品,编号为\(1-2n\),存在唯一的两两配对关系,即有\(n\)种物品 有一个盒子,初始为空,盒子上会显示里面存在的物品种类数\(C\) 你每次操作可以将一个物品从盒 ...

  10. 【BZOJ4475】 [Jsoi2015]子集选取

    题目描述 数据范围 \(1\leq N,K \leq 10^9\) \(solution\) 集合S中每个元素互不影响,不妨依次考虑其中一个元素在三角形中的出现情况 问题转化为一个\(0/1\)的三角 ...