Question

Count the number of prime numbers less than a non-negative number, n.

Solution 1

Naive way, to check each number one by one. If a number i is prime, then for x from o to (i - 1), i % x != 0. Time complexity O(n^2).

Solution 2

Sieve of Eratosthenes 算法:

由于一个合数总是可以分解成若干个质数的乘积,那么如果把质数(最初只知道2是质数)的倍数都去掉,那么剩下的就是质数了。
例如要查找100以内的质数,首先2是质数,把2的倍数去掉;此时3没有被去掉,可认为是质数,所以把3的倍数去掉;再到5,再到7,7之后呢,因为8,9,10刚才都被去掉了,而100以内的任意合数肯定都有一个因子小于10(100的开方),所以,去掉,2,3,5,7的倍数后剩下的都是质数了。

 public class Solution {
public int countPrimes(int n) {
if (n <= 2)
return 0;
boolean[] primes = new boolean[n];
primes[0] = false;
for (int i = 2; i < n; i++) primes[i] = true;
for (int i = 2; i < Math.sqrt(n); i++) {
if (primes[i] == true) {
int j = 2;
while (i * j < n) {
primes[i * j] = false;
j++;
}
}
}
int result = 0;
for (int i = 2; i < n; i++) {
if (primes[i])
result++;
}
return result;
}
}

Count Primes 解答的更多相关文章

  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. 使用itextsharp创建PDF文档——图片集合

    文档管理系统中 ,扫描模块将文档或证件扫描后.为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存. 这里我们就需要PDF生成工具了.你可以在这里下载.PDFCreator 主要使用了开源工具 ...

  2. python高级编程之超类02:super的缺陷

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #当使用多重继承层次结构时,再使用super的时候是非常危险的,主要 ...

  3. 《招聘一个靠谱的iOS》面试题参考答案(下)

    相关文章: <招聘一个靠谱的iOS>面试题参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外 ...

  4. web前端 - 模态对话框

    代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  5. Flashback version/Transaction Query,FlashbackTable

    Flashback version Query相对于Flashback Query 只能看到某一点的对象状态, Oracle 10g引入的Flashback Version Query可以看到过去某个 ...

  6. MongoDB学习笔记05

    count 返回集合中文档数量文档数量 db.foo.count() db.foo.count({}) distinct用来找出给定键的所有不同的值,使用时必须指定集合和键 db.runCommand ...

  7. Vim知识点收集

    (注意: 只记录工作中实际使用的命令) 删除带有pattern的所有行    :g/pattern/d 删除不带pattern的所有行   :g!/pattern/d 匹配red和blue,无次序   ...

  8. 0128——手势Gesture

    UIGestureRecognizer: 1.locationinView 获取手势在某个视图里面的坐标位置 2.delegate监听手势的行为 3.state状态 开始:UIGestureRecog ...

  9. JS闭包(一)

    闭包是指有权访问另一个函数作用域中的变量的函数. 创建闭包的常见方法:在一个函数内部创建另一个函数. 对彻底理解闭包,需要知道如何创建作用域链以及作用域链有什么作用的细节. 闭包的功能: 保存函数执行 ...

  10. (转) Eclipse - Python - Installation of PyDev with a Python Hello World tutorial

    Once you finished your installation of Python on your Windows OS,  GNU/Linux or Mac OS, let me tell ...