寻找比n小的所有质数的方法。

2是质数, 2*i都是质数,同样3是质数,3*i也都是质数

代码如下

 int n;
vector<char> prime (n+, true);
prime[] = prime[] = false;
for (int i=; i<=n; ++i)
if (prime[i])
if (i * 1ll * i <= n)
for (int j=i*i; j<=n; j+=i)
prime[j] = false;

Algorithm: Sieve of Eratosthenes的更多相关文章

  1. algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )

    Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...

  2. 使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)

    Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime ...

  3. 埃拉托色尼筛法(Sieve of Eratosthenes)求素数。

    埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现, ...

  4. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  5. Sieve of Eratosthenes时间复杂度的感性证明

    上代码. #include<cstdio> #include<cstdlib> #include<cstring> #define reg register con ...

  6. “计数质数”问题的常规思路和Sieve of Eratosthenes算法分析

    题目描述 题目来源于 LeetCode 204.计数质数,简单来讲就是求"不超过整数 n 的所有素数个数". 常规思路 一般来讲,我们会先写一个判断 a 是否为素数的 isPrim ...

  7. [Algorithm] Finding Prime numbers - Sieve of Eratosthenes

    Given a number N, the output should be the all the prime numbers which is less than N. The solution ...

  8. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes

    原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...

  9. What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?

    QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...

随机推荐

  1. sudo如何保持当前用户的环境变量?

    现象,我在/etc/profile里设置全局代理,然后使用命令 1.curl http://www.baidu.com  走代理 2.sudo curl http://www.baidu.com 并没 ...

  2. 在Delphi中应用AOP实现日志功能

    AOP现在很火,网上有这许多支持AOP的框架,对于Delphi来说同样也有MeAOP.不过觉得这些框架太复杂了. 现在有一个系统,基本上都快结束了,整体上当然是没有采用什么AOP的框架.对于这样的系统 ...

  3. Java面向对象编程(二)

    上一篇博文里总结了面向对象三大特性在Java中的体现.如今谈一谈Java中的抽象类,接口,内部类等特性. 一. 抽象类 public abstract class Shape { public int ...

  4. 19. Spring Boot 添加JSP支持【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52017140 这个部分比较复杂,所以单独创建一个工程来进行讲解: 大体步骤: (1)     ...

  5. An internal error occurred Exception caught during execution of commit command

    在工程目录下找到 .git 文件夹 ,找到里面的 index.lock 文件,删掉再commit

  6. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  7. 【Excle数据透视表】如何创建非共享缓存的数据透视表

    一般情况下,利用同一个数据源创建多个数据表时,默认创建的是共享缓存的数据透视表.刷新一个数据透视表时会影响其他数据透视表的展示结果. 解决方案 创建非共享缓存的多个数据透视表 步骤一 单击工作表数据任 ...

  8. 可以添加自定义的Select控件

    1.控件dom <select name="WebSiteTarget" id="WebSiteTarget" class="w1" ...

  9. Cocos2d-x 更改文字换行风格 ( cocos2dx change line )

    Cocos2dx change line 在 cocos2dx change line 的实现中,我们能够简单的使用 dimensions属性控制换行.使用它仅仅需将相应的參数值传入构造函数,或者调用 ...

  10. caffe-ubuntu1604-gtx850m-i7-4710hq----bvlc_reference_caffenet.caffemodel

    bvlc_reference_caffenet.caffemodel --- name: BAIR/BVLC CaffeNet Model caffemodel: bvlc_reference_caf ...