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 ...
随机推荐
- MySQL性能优化必备25条
1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...
- Angular——内置服务
$location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...
- golang zip 压缩,解压(含目录文件)
每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...
- CAD控件:QT开发使用控件入门
1. 环境搭建: 3 1.1. 安装Qt 3 1.2. 安装Microsoft Windows SDK的调试包 6 2. QT中使用MxDraw控件 7 1.3. 引入控件 7 3. 打开DWG文件 ...
- luogu P4137 Rmq Problem / mex 主席树 + 思维
Code: #include<bits/stdc++.h> #define maxn 200001 using namespace std; void setIO(string s) { ...
- 模板—treap
#include<iostream> #include<cstdio> #include<cstdlib> #define INF 0x7fffffff using ...
- 关于mybatis返回值resultType为空的问题
假设数据库中一个user表 此时只有id为1的数据,当我们查询id为2的年龄时的时候返回值为null 但是在mybatis中预定义UserMapper.xml中 <select id=" ...
- Nginx + Lets'encrypt 实现HTTPS访问七牛空间资源
上一篇文章 为七牛云存储空间绑定自定义域名,并使用七牛云提供的免费SSL证书,将自定义加名升级为HTTPS 我们提到利用七牛的免费SSL证书,将自定义加名升级为HTTPS的方法. 不知道有没有小伙伴会 ...
- 【模板】网络流-最大流 Dinic
洛谷 3376 #include<cstdio> #include<algorithm> #include<cstring> #define N 10010 #de ...
- wannafly-day1 Problem B-Board
思路:这个题队友过的,我的思路是枚举行和列,将除了要求位置初始0,每行最小值相减,每列最小值相减,直到除了要求的位置,别的位置都为零,则那个位置取绝对值就行了,有点麻烦应该能过,但是他没有用我给的想法 ...