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 ...
随机推荐
- leetcode_day02
任务二:删除排序数组中的重复项 原文链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 最开始的解决思路: ...
- python作业:模拟登陆(第一周)
模拟登陆作业需求: 1. 用户输入帐号密码进行登陆 2. 用户信息保存在文件内 3. 用户密码输入错误三次后锁定用户 额外实现功能: 1.提示输入错误次数 2.输入已锁定用户会提示 3.用户不存在会提 ...
- Leetcode with Python -> Sort
349. Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Ex ...
- PAT 1070 结绳
https://pintia.cn/problem-sets/994805260223102976/problems/994805264706813952 给定一段一段的绳子,你需要把它们串成一条绳. ...
- linux下如何修改进程优先级?
linux下的进程调度优先级是从-20到19,一共40个级别,数字越大,表示进程的优先级越低.默认时候,进程的优先级是0.查看进程优先级有两个办法:ps和top. 改变进程的优先级的方法有两种: 1, ...
- 旅行规划(travel)
题目描述 OIVillage 是一个风景秀美的乡村,为了更好的利用当地的旅游资源,吸引游客,推动经济发展,xkszltl 决定修建了一条铁路将当地 nnn 个最著名的经典连接起来,让游客可以通过火车从 ...
- Java面试题之hashmap中用什么hash算法解决碰撞的?
查了一下源码(jdk8),记录一下吧,能记住就记一下吧! static final int hash(Object key) { int h; return (key == null) ? 0 : ( ...
- SHH框架的搭建
建立一个Web项目,然后导入如下包 l struts2包:在struts2-blank.war下的lib目录下,以及struts-2.3.15.1\lib下的struts和spring整合的插件包s ...
- for 循环里的i++
写代码的时间也不短了,今天看快速排序的算法的时候才去更深层次得理解... for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 (代码块)开始前执行 语句 2 定义运 ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...