题意:给一个数n,返回小于n的素数个数。

思路:设数字 k =from 2 to sqrt(n),那么对于每个k,从k2开始,在[2,n)范围内只要是k的倍数的都删掉(也就是说[k,k2)是不用理的,若能被筛掉早就被筛了,保留下来的就是素数)。最后统计一下[2,n)内有多少个还存在的,都是素数。

  要注意,如果k已经被筛掉了,那么不用再用它来删别人了,因为已经被筛掉,那么现在比k2大的且是k的倍数,都已经被干掉了。

 class Solution {
public:
int countPrimes(int n) {
bool* isPrime =new bool[n] ; memset(isPrime,,n); for(int i=; i*i<n; i++)
{
if(!isPrime[i]) continue;
for(int j=i*i; j<n; j+=i) isPrime[j]=;
}
int cnt=;
for(int i=; i<n; i++) if(isPrime[i]) cnt++;
return cnt;
}
};

AC代码

LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)的更多相关文章

  1. [LeetCode] Count Primes 质数的个数

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

  2. leetcode 204题求素数个数

        Description: Count the number of prime numbers less than a non-negative number, n 提示晒数法: http:// ...

  3. 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)

    Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...

  4. 算法笔记_012:埃拉托色尼筛选法(Java)

    1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...

  5. Java实现埃拉托色尼筛选法

    1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...

  6. [leetcode] Count Primes

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

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

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

  8. 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)

    引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...

  9. HDU 5901 Count primes 大素数计数

    题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...

随机推荐

  1. C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.

    Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...

  2. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. Android 检查设备是否存在 导航栏 NavigationBar

    尊重原创.尊重作者,转载请标明出处: http://blog.csdn.net/lnb333666/article/details/41821149 目前也没有可靠的方法来检查设备上是否有导航栏.可以 ...

  4. iOS多线程的初步研究(四)-- NSTimer

    理解run loop后,才能彻底理解NSTimer的实现原理,也就是说NSTimer实际上依赖run loop实现的. 先看看NSTimer的两个常用方法: + (NSTimer *)timerWit ...

  5. springMVC视频教程

    http://edu.51cto.com/index.php?do=lession&id=42165

  6. Assembly(c#中简单说明[转]

    什么是Assembly(程序集)?Assembly是一个包含来程序的名称,版本号,自我描述,文件关联关系和文件位置等信息的一个集合.在.net框架中通过Assembly类来支持,该类位于System. ...

  7. lintcode:打劫房屋

    题目 打劫房屋 假设你是一个专业的窃贼,准备沿着一条街打劫房屋.每个房子都存放着特定金额的钱.你面临的唯一约束条件是:相邻的房子装着相互联系的防盗系统,且 当相邻的两个房子同一天被打劫时,该系统会自动 ...

  8. WordPress主题制作教程6:常用模版标签

    在wordpress中,模板标签指一些字段,比如标题,内容,作者,发布日期,评论数等等,获取静态值和循环里面经常使用. 输出模板标签一般有两种方式:the_yourtag() 输出标签值和get_th ...

  9. sdk manager更新超时 time out

    sdk manager选择菜单tools->options,勾选下面的force https......重启sdk manager

  10. WAS集群服务的关闭与启动

    WAS集群服务的关闭与启动 欢迎转载,转载时请务必注明出处(http://blog.csdn.net/huangyanlong),否则作者保留追究版权法律责任. 表述有错误之处,请您留言或邮件(hyl ...