题目大意

https://leetcode.com/problems/count-primes/description/

204. Count Primes

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

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

题目大意:
统计小于非负整数n的素数的个数

提示:n的范围是100,000到5,000,000

解题思路

参考文献:

一共有多少个素数?(https://primes.utm.edu/howmany.html)

埃拉托斯特尼筛法 (http://en.wikipedia.org/wiki/Sieve_of_Eratostheneshttp://open.163.com/movie/2012/10/0/6/M99VJKUHC_M9ENDUB06.html

伪代码

 Input: an integer n > 1.

 Let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true. for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n:
A[j] := false. Output: all i such that A[i] is true.

Python解法

起初Python的时间限制过于严格,采用Python解题对于测试样例5000000总是返回Time Limit Exceeded,后来管理员放宽了Python的时限。

class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
is_prime = [True] * max(n, 2)
is_prime[0], is_prime[1] = False, False
x = 2
while x * x < n:
if is_prime[x]:
p = x * x
while p < n:
is_prime[p] = False
p += x
x += 1
return sum(is_prime) def countPrimes_v0(self, n):
"""
:type n: int
:rtype: int
"""
is_prime = [True] * max(n, 2)
is_prime[0], is_prime[1] = False, False
for x in range(2, int(n ** 0.5) + 1):
if is_prime[x]:
p = x * x
while p < n:
is_prime[p] = False
p += x
return sum(is_prime)

Java解法

public class Solution {
public int countPrimes(int n) {
boolean notPrime[] = new boolean[n + 2];
notPrime[0] = notPrime[1] = true;
for (int i = 2; i * i < n; i++) {
if (!notPrime[i]) {
int c = i * i;
while (c < n) {
notPrime[c] = true;
c += i;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (!notPrime[i])
ans ++;
}
return ans;
}
}

参考:http://bookshadow.com/weblog/2015/04/27/leetcode-count-primes/

[leetcode] 204. Count Primes 统计小于非负整数n的素数的个数的更多相关文章

  1. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

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

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

  3. LeetCode 204 Count Primes

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

  4. Leetcode 204 Count Primes 数论

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

  5. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes

    原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...

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

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

  7. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  8. Java [Leetcode 204]Count Primes

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

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

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

随机推荐

  1. PAT 1112 Stucked Keyboard[比较]

    1112 Stucked Keyboard(20 分) On a broken keyboard, some of the keys are always stucked. So when you t ...

  2. 【Lua】LDoc生成Lua文档工具的使用

    参考资料: http://my.oschina.net/wangxuanyihaha/blog/188909   LDoc介绍:     LDoc是一个Lua的文档生成工具,过去,比较常用的Lua生成 ...

  3. 002-字段不为null

    1.尽量不要在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,强烈建议where涉及的列,不要留空,创建表时赋予初始值. 比如 select id from ...

  4. MyBatis—mybatis-config.xml模板

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC & ...

  5. try with resources简洁的异常捕获机制

    通过前篇的<Java文件IO流的操作总结>,我们知道了基本输入输出流的使用方式,但是每次都需要在finally处关闭流资源,这样操作起来既啰嗦又麻烦,有没有更简洁的方式呢?本篇就来讲解jd ...

  6. nodejs+express工程 在npm install之后或使用npm install bootstrap命令安装bootstrap之后

    nodejs+express工程 在npm install之后或使用npm install bootstrap命令安装bootstrap之后引入bootstrap文件 如果你的静态资源存放在多个目录下 ...

  7. 20145219《网络对抗》MSF基础应用

    20145219<网络对抗>MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:把实现设置好的东西送到要攻击的主机里. payl ...

  8. 重器--biomart

    biomart 重器 biomaRt工具包的作用在于它可以轻松地完成的在多个生物学数据库上繁琐地检索,获取相关数据在不同数据库间的关联.

  9. python 获取进程执行的结果

    import subprocessp = subprocess.Popen([r'ls'],stdout=subprocess.PIPE) result = p.stdout.read()print( ...

  10. shell 判断变量是否为空

    一句话判断 [ ! $a ] && echo "a is null" 1.判断变量 read -p "input a word :" word ...