题目大意

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. django之中间件、缓存、信号、admin内置后台

    目录: 中间件 缓存 信号 admin后台 一.中间件 1.什么是中间件? 中间件是一个.一个的管道,如果相对任何所有的通过Django的请求进行管理都需要自定义中间件 中间件可以对进来的请求和出去的 ...

  2. python 操作 mysql 数据库 datetime 属性字段为 0000-00-00 00:00:00 的问题

    撇开 sqlalchemy, 先讲 MySQLdb 和 pymysql mysql 版本 mysql  Ver 14.14 Distrib 5.1.73 新建一个测试表 test, 结构如下: mys ...

  3. visio基础教程(一)

  4. viewport 编写 iPhone Friendly 的 Web 应用程序

    在了解到iPhone的一些常见布局法后,我们就可以开始着手编写一个真正能在iPhone上跑的页面了.小声说一句,之前我说要布局讨论完了,要进入交互逻辑开发,后来细心一想发现不行,有些东西不讲的话将会对 ...

  5. window 下相关命令

    1. 启动window服务(各种应用启动设置的地方)命令方式: 1). window 按钮(输入CMD的地方)处输入:services.msc ,然后执行.   // 输入命令正确,上面的待选框中会出 ...

  6. animation CSS3动画总结

    最近一个小游戏项目用到了CSS3的动画属性,例如transition.transform.animation.经过三个星期,终于做完了,利用周末好好梳理总结一下. keyframes这个属性用来定义一 ...

  7. 在Qt中如何编写插件,加载插件和卸载插件(转)

    Qt提供了一个类QPluginLoader来加载静态库和动态库,在Qt中,Qt把动态库和静态库都看成是一个插件,使用QPluginLoader来加载和卸载这些库.由于在开发项目的过程中,要开发一套插件 ...

  8. DB开发之oracle存储过程

    1. 存储过程格式 /* Formatted on 2011/1/17 13:20:44 (QP5 v5.115.810.9015) */ CREATE OR REPLACE procedure pr ...

  9. python常见容器属性和方法

    `````字符串中反斜杠字符表 转义格式    意义 \'  单引号(') \"  双引号(") \\  反斜杠(\ ) \n  换行 \r  返回光标至行首 \f  换页 \t ...

  10. windows下通过ping和tracert工具来测试网站访问速度

    一.环境 OS: windows 二.步骤 2.1.ping mirrors.163.com Pinging mirrors.163.com [23.111.1.151] with 32 bytes ...