LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)

题意:给一个数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 求素数个数(埃拉托色尼筛选法)的更多相关文章
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- leetcode 204题求素数个数
Description: Count the number of prime numbers less than a non-negative number, n 提示晒数法: http:// ...
- 使用埃拉托色尼筛选法(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 ...
- 算法笔记_012:埃拉托色尼筛选法(Java)
1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...
- Java实现埃拉托色尼筛选法
1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
随机推荐
- Using an Interface as a Type
When you define a new interface, you are defining a new reference data type. You can use interface n ...
- ZOJ3550 Big Keng(三分)
题意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少.(不算上表面).一开始拿到题以为可以YY出一个结果,就认为它是圆锥 ...
- iOS多线程的初步研究(三)-- NSRunLoop
弄清楚NSRunLoop确实需要花时间,这个类的概念和模式似乎是Apple的平台独有(iOS+MacOSX),很难彻底搞懂(iOS没开源,呜呜). 官网的解释是说run loop可以用于处理异步事件, ...
- To Use Ubuntuubunt
1.rename PC; modify the /etc/hostname. 2.Use root account; 1.set the password for root account: sudo ...
- Android开发者应该深入学习的10个开源应用项目
Android 开发带来新一轮热潮让很多移动开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的 智慧和创造力.学习开源代码是掌握技术的 ...
- root 授权
错误:The user specified as a definer ('root'@'%') does not exist 解决: grant all privileges on *.* to ro ...
- DB2对年份的处理Year()
public DataSet GetCustomerAllocListByQC(CustomerAllocQueryDataContract aQC) { StringBuilder sql = ne ...
- WCF分布式开发步步为赢(7):WCF数据契约与序列化
本节继续学习WCF分布式开发步步为赢(7):WCF数据契约与序列化.数据契约是WCF应用程序开发中一个重要的概念,毫无疑问实现客户端与服务端数据契约的传递中序列化是非常重要的步骤.那么序列化是什么?为 ...
- poj 2349(最小生成树应用)
题目链接:http://poj.org/problem?id=2349 思路:由于有S个专门的通道,我们可以先求一次最小生成树,然后对于最小生成树上的边从大到小排序,前S-1条边用S-1个卫星通道连接 ...
- 【转】SQL Server T-SQL写文本文件
原文:http://www.nigelrivett.net/SQLTsql/WriteTextFile.html The are several methods of creating text fi ...