Count Primes -- LeetCodes (primality test)
Description:
Count the number of prime numbers less than a non-negative number, n.
思路:这题第一种思路是写一个is_prime函数,来对每一个数验证一次是否是prime。
这个方法在这个题里表现不快,但也是一个学习is_prime函数的机会。
首先,验证一个数是不是prime,只需要用小于它的所有正整数全除一遍看是否能整除就行。实际上,我们只需要试验到sqrt(n),不需要到n-1。因为假如p * q = n的话,p和q是关于sqrt(n)对称的。
再然后,所有数都可以表示成6 * k + i的形式,其中k为整数,i = -1, 0, 1, 2, 3, 4。当i = 0, 2, 4时该数一定是偶数,不可能是prime。当i = 3时,这个数能被3整除,也不是。因此,若一个数是prime,除了2和3,它一定能被表示成 6 * k +/- 1的形式。
由此,我们获得了一个检测n是否是prime的思路:先验证n能否被2或者3整除。然后验证n能否被不大于sqrt(n)的所有能表示成6 * k +/- 1形式的数整除。
bool is_prime(int n)
{
if (n <= ) return false;
else if (n <= ) return true;
else if (n % == || n % == )
return false;
for (int i = ; i * i <= n; i += )
if (n % i == || n % (i + ) == )
return false;
return true;
}
接下来介绍这个题里能用到的解法。
思路是我们先使用一个大小为n的数组,然后从2开始,将所有2的倍数全部标记成非prime。然后是3,将所有3的倍数全部标记成非prime。这里要注意的是,因为2 * 3已经在2的倍数那一步标注过了,因此这里从3的3倍开始。到4的时候,因为4已经被标注为非prime了,因此直接跳过。以此类推,将所有i从i倍开始的所有倍数标注为非prime。这个过程一直持续到i等于sqrt(n)。
class Solution {
public:
int countPrimes(int n) {
vector<bool> tab(n, true);
for (int i = ; i * i < n; i++)
if (tab[i])
for (int j = i; i * j < n; j++)
tab[i * j] = false;
int res = ;
for (int i = ; i < n; i++)
if (tab[i]) res++;
return res;
}
};
Count Primes -- LeetCodes (primality test)的更多相关文章
- [leetcode] Count Primes
Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- LeetCode_204. Count Primes
204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- python tarfile模块打压缩包,arcname的用法
D:\szh\noses文件夹下有子文件夹和文件 with tarfile.open('E:\\szh.tar', "w") as tar: tar.add('D:\\ ...
- Wordpress 后台文章编辑区添加模板选择功能
功能:后台编辑文章时,可以选择文章使用的模板,效果如下图: 操作步骤: <?php /** * Template Name: kbsingle full * Add by Ryan 3/18/2 ...
- 1086 Tree Traversals Again (25 分)(二叉树的遍历)
用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历 由题棵知道:push是先序遍历 pop是中序遍历 #include<bits/stdc++.h> using name ...
- RMAN 增量备份级别说明
通过Bat批处理调用RMan是我们定时备份数据库的好帮手,但是RMan的备份级别需要我们好好了解一下. RMAN备份全为全备和增量备份 增量备份:分为0 1 2级 ORACLE官方解释: A leve ...
- C#程序怎么写,效率高
文章:改善C#程序,提高程序运行效率的50种方法 文章:编写高效率的C#代码
- reinterpret_cast and const_cast
reinterpret_cast reinterpret意为“重新解释” reinterpret_cast是C++中与C风格类型转换最接近的类型转换运算符.它让程序员能够将一种对象类型转换为另一种,不 ...
- StringBuilder_学习笔记
参考:https://www.jianshu.com/p/160c9be0b132 连接符号 "+" 本质 字符串变量(非final修饰)通过 "+" 进行拼接 ...
- Lambda表达式使用2
1.概述 本篇主要介绍lambda中常用的收集器,收集器的作用就是从数据流中生成需要的数据接口. 最常用的就是Collectors.toList(),只要将它传递给collect()函数,就能够使用它 ...
- firefox解决flash崩溃
1.地址栏输入 about:config 2.查找dom.ipc.plugins.flash.subprocess.crashreporter.enabled 更改为false
- C++自带向量_vector_C++
vector 向量,是C++自带的一种容器,其实就是一个升级版的数组 因为它使用的是动态空间,所以当我们不确定数组空间的时候可以使用它 若要使用需打开头文件 #include<vector> ...