leetCode(49):Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
推断一个数是否是质数主要有下面几种方法:
1)直接用该数除于全部小于它的数(非0。1),假设均不能被它整除,则其是质数。
2)除以小于它一半的数。由于大于其一半必然是无法整除。假设均不能被它整除。则其是质数;
3)除以小于sqrt(a)的数,原因例如以下:
除了sqrt(a)之外,其它的两数乘积为a的,一定是比个比sqrt(a)小,一个比sqrt(a)大。所以推断到sqrt(a)就能够了,由于还有一半就是刚才做除法的商的那部份。
4)除以小于sqrt(a)的质数就可以。
由于质数不能被除自身和1外的全部数整除。非质数则不然,且其必定能被某一质数整除。假设一个数能被某一非质数整除。则其必定能被组成这一非质数的最小质数整数。
从上能够看出,推断一个数是否是质数。其计算量取决于整除的个数。上述四种方法,除数逐渐变少。
通过以上分析,这是本人自己的程序:
class Solution {
public:
int countPrimes(int n) {
if (n == 0 || n == 1 || n==2)
return 0; vector<int> vec;//质数容器 vec.push_back(2);
for (int i = 3; i < n; ++i)
{
int j = 0;
int length=vec.size();
for (; j<length && vec[j]*vec[j]<i; ++j)//除数是小于sqrt(i)的全部质数
{
if (i%vec[j] == 0)
break;
}
if (vec[j]*vec[j]>i)
vec.push_back(i);
}
return vec.size();
}
};
以下是在网上找到一个程序,非常具体:
这道题给定一个非负数n,让我们求小于n的质数的个数,题目中给了充足的提示。解题方法就在第二个提示埃拉托斯特尼筛法Sieve
of Eratosthenes中。这个算法的步骤例如以下图所看到的,我们从2開始遍历到根号n,先找到第一个质数2,然后将其全部的倍数全部标记出来。然后到下一个质数3。标记其全部倍数。一次类推,直到根号n,此时数组中未被标记的数字就是质数。我们须要一个n-1长度的bool型数组来记录每一个数字是否被标记,长度为n-1的原因是题目说是小于n的质数个数。并不包含n。
然后我们用两个for循环来实现埃拉托斯特尼筛法,难度并非非常大。代码例如以下所看到的:

class Solution {
public:
int countPrimes(int n) {
vector<bool> num(n - 1, true);
num[0] = false;
int res = 0, limit = sqrt(n);
for (int i = 2; i <= limit; ++i) {
if (num[i - 1]) {
for (int j = i * i; j < n; j += i) {//为什么要从i*i開始,由于小于i*i的数可能已经被小于i的数整除了。假设没有,那它就是质数
num[j - 1] = false;
}
}
}
for (int j = 0; j < n - 1; ++j) {
if (num[j]) ++res;
}
return res;
}
};

leetCode(49):Count Primes的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
随机推荐
- Linux系统资源监控--linux命令、nmon和spotlight
前言: 系统资源监控一般监控系统的CPU,内存,磁盘和网络.系统分为windows和Linux.本篇主要记录Linux. Linux系统资源监控常用命令及工具 一.常用命令:top.free.iost ...
- CherryPy 入门
CherryPy是一个Python的HTTP框架,可以用Python来处理HTTP请求然后返回结果. 1. 安装 可以去这个地址下载 CherryPy-3.1.2.win32.exe .或者去这个链接 ...
- Ps 快捷键全解
一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)矩形.椭圆选框工具 [M]移动工具 [V]套索.多边形套索.磁性套索 [L]魔棒工具 [W]裁剪工具 [C]切片工具.切片选择 ...
- HDU_1227_Fast Food_动态规划
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1227 Fast Food Time Limit: 2000/1000 MS (Java/Others) ...
- Python的伪造数据库:Faker
faker 是一个可以让你生成伪造数据的Python包,在软件需求.开发.测试过程中常常需要利用一些假数据来做测试,这种时候就可以使用 Faker 来伪造数据从而用来测试. 一.Faker安装 pip ...
- mac系统,鼠标移动太慢
to check your speed: defaults read -g com.apple.mouse.scaling to set your speed defaults write -g co ...
- Redis系列(十)--集群cluster
在之前学习了Master-Slave.Sentinel模式,但是在某些情况下还是无法满足系统对QPS等要求,这时候就需要Cluster,Redis3.0支持了cluster 一.为什么使用Cluste ...
- jquery 五星评价(图片实现)
1111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- (C/C++学习)16.函数指针
说明:函数指针,顾名思义就是指向函数的指针.C/C++中函数名的本质其实就是一段代码段空间的首地址. 1.定义 如下的 pf 就是一个函数指针,指向所有返回类型为 int,并带有两个 const in ...
- 每日命令:(10)cat
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...