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)的更多相关文章

  1. [leetcode] Count Primes

    Count Primes Description: Count the number of prime numbers less than a non-negative number, n click ...

  2. 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 ...

  3. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  4. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  5. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. LeetCode_204. Count Primes

    204. Count Primes Easy Count the number of prime numbers less than a non-negative number, n. Example ...

  7. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  8. 204. Count Primes - LeetCode

    Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...

  9. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

随机推荐

  1. UIAutomator2、Appium、Robotium搭建环境与框架对比

    UIAutomator2.Appium.Robotium搭建环境与框架对比 一.框架介绍 Appium 特点 appium 是一个自动化测试开源工具,支持 iOS 平台和 Android 平台上的原生 ...

  2. python 学习分享-线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  3. 孤荷凌寒自学python第三十天python的datetime.datetime模块

     孤荷凌寒自学python第三十天python的datetime.datetime模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) datetime.datetime模块包含了:datet ...

  4. Qt(1)

    Qt Qt开发图形界面软件,可以跨win.linux.mac平台.移动端,使用c++开发 Qt采用所见即所得的UI设计(UI设计和代码是联动的),GUI界面编辑信号和槽,由开发环境自动生成c++代码, ...

  5. python使用工具简介介绍

    我从研究生开学以来就开始在学python,现在来简单分享下一些基本的使用命令和快捷方式 Pycharm: 运行程序 ctrl+alt+F10 删除一行ctrl+D 注释ctrl+/ 安装python所 ...

  6. VC调试篇:减少运行时错误,中断所有异常

    问题简述 我在Win7下写的MFC程序,想让它在winXP下运行.一般情况下,如果所有的依赖库都可以在XP下运行的话,那么在XP下运行时没问题的.但是,结果却... 本来程序在win7下运行得好好的, ...

  7. poj 1062 昂贵的聘礼 (最短路径)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33365   Accepted: 9500 Descriptio ...

  8. P2294 [HNOI2005]狡猾的商人

    题目描述 输入输出格式 输入格式: 从文件input.txt中读入数据,文件第一行为一个正整数w,其中w < 100,表示有w组数据,即w个账本,需要你判断.每组数据的第一行为两个正整数n和m, ...

  9. 1031. 高一学堂 (at)

    题目描述 在美丽的中山纪念中学里面,有一座高一学堂.所谓山不在高,有仙则名:水不在深,有龙则灵.高一学堂,因为有了yxr,就成了现在这个样子 = =. 由于yxr的语言太过雷人,每次他发微往往都会有一 ...

  10. Class-dump

    What is class-dump? This is a command-line utility for examining the Objective-C runtime information ...