素数的判断:

 #include<math.h>
bool IsPrime(int n)
{
if(n <= )
return false;
int sqr = (int)sqrt(1.0*n);
for(int i=; i<=sqr; i++)
if(n%i==) return false;
return true;
}

获取素数表:

 const int M=;
vector<int> prime;
bool isPrime[M]={}; void FindPrime()
{
for(int i=; i<M; i++)
if(IsPrime(i))
{
prime.push_back(i);
isPrime[i] = ;
}
}

埃氏筛法(Eratosthenes):

 #include<cstdio>
#include<algorithm>
using namespace std;
const int M=;
int isPrime[M]; void Eratosthenes()
{
fill(isPrime, isPrime+M, );
isPrime[]=;
isPrime[]=;
for(int i=; i<M; i++)
if(isPrime[i] == )
{
printf("%d ", i);
for(int j=*i; j<M; j+=i)
isPrime[j] = ;
} } int main()
{
Eratosthenes();
return ;
}
  • 时间复杂度:O( Nlog(logN) )
  • 第一个测出地球周长的男人-。-

欧氏筛法(Euler):

 #include<cstdio>
#include<vector>
using namespace std;
const int M=;
int isPrime[M]; void Euler()
{
fill(isPrime, isPrime+M, );
isPrime[]=;
isPrime[]=;
vector<int> prime;
for(int i=; i<M; i++)
{
if(isPrime[i])
{
prime.push_back(i);
printf("%d ", i);
}
for(int j=; j<prime.size(); j++)
{
if(i*prime[j] > M)
break;
isPrime[i*prime[j]] = ;
if(i%prime[j] == )
break;
}
}
} int main()
{
Euler();
return ;
}
  • 时间复杂度:O(N)
  • 欧拉待过的俄法德,都曾是世界最强的国家,如果当初欧拉来中国,是不是咱们就年年ACM总冠军了-。-

素数(Prime)的更多相关文章

  1. 『素数 Prime判定和线性欧拉筛法 The sieve of Euler』

    素数(Prime)及判定 定义 素数又称质数,一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数,否则称为合数. 1既不是素数也不是合数. 判定 如何判定一个数是否是素数呢?显然,我 ...

  2. Java 素数 prime numbers-LeetCode 204

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

  3. [Swift]LeetCode866. 回文素数 | Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  4. CD0J/POJ 851/3126 方老师与素数/Prime Path BFS

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 Descri ...

  5. POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...

  6. LightOj 1197 - Help Hanzo(分段筛选法 求区间素数个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 题意:给你两个数 a b,求区间 [a, b]内素数的个数, a and b ( ...

  7. poj 2739 Sum of Consecutive Prime Numbers 解题报告

    题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...

  8. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  9. 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)

    关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...

  10. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

随机推荐

  1. HDU - 3040 - Happy Girls

    先上题目: Happy Girls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. Atomic operations on the x86 processors

    On the Intel type of x86 processors including AMD, increasingly there are more CPU cores or processo ...

  3. 1009MySQL数据库InnoDB存储引擎Log漫游

    00 – Undo Log Undo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版本并发控制(简称:MVCC). - 事务的原子性(Atom ...

  4. Sigma Function 数学 因子求和

    Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma ...

  5. HDU 4515

    刷水完毕,年月日,日日日日日日日日日日日日日日日日日日 #include <stdio.h> ,,,,,,,,,,,,}; ,M = ,D = ; int leap(int y) { == ...

  6. 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  7. C++一些知识难点

    什么是"引用"?申明和使用"引用"要注意哪些问题? 答:引用就是某个目标变量的"别名"(alias).相应用的操作与对变量直接操作效果全然同 ...

  8. ssh无法连接到远端Ubuntu的解决方法

    近日,饱受无法远程登录到新安装在VMWare上的Ubuntu虚拟机,现在发现问题所在.故记录此问题的解决方式,以备后用. 一.远程登录虚拟机的准备: Ubuntu虚拟机的联网方式应该选择Bridged ...

  9. luogu2441 角色属性树

    题目大意:维护一个可查询.修改的树,查询的是一个节点的:离它距离最近的.组成两个节点Key值的质因数存在交集的.祖先节点:修改是修改一个节点的key值. 如果组成两个Key值的质因数存在交集,则两个数 ...

  10. python spark 求解最大 最小 平均

    rdd = sc.parallelizeDoubles(testData); Now we’ll calculate the mean of our dataset.   1 LOGGER.info( ...