素数(Prime)
素数的判断:
#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)的更多相关文章
- 『素数 Prime判定和线性欧拉筛法 The sieve of Euler』
素数(Prime)及判定 定义 素数又称质数,一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数,否则称为合数. 1既不是素数也不是合数. 判定 如何判定一个数是否是素数呢?显然,我 ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- [Swift]LeetCode866. 回文素数 | Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- CD0J/POJ 851/3126 方老师与素数/Prime Path BFS
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9982 Accepted: 5724 Descri ...
- POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...
- LightOj 1197 - Help Hanzo(分段筛选法 求区间素数个数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 题意:给你两个数 a b,求区间 [a, b]内素数的个数, a and b ( ...
- poj 2739 Sum of Consecutive Prime Numbers 解题报告
题目链接:http://poj.org/problem?id=2739 预处理出所有10001以内的素数,按照递增顺序存入数组prime[1...total].然后依次处理每个测试数据.采用双重循环计 ...
- HDU_2136——最大质因数,素数筛选法
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)
关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...
- 解题报告:poj2689 Prime Distance
2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...
随机推荐
- 如何彻底卸载系统自带的IE浏览器
IE浏览器是windows系统上自带的浏览器,有时我们想要用其他的浏览器,例如chrome,卸载IE浏览器,那么应该如何卸载呢?下面就以win7上的IE9为例,告诉大家如何卸载IE浏览器. 方法/步骤 ...
- codevs——T1169 传纸条
http://codevs.cn/problem/1169/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- js为字符串编码
js 提供了两组函数来进行字符串的编码与解码:escape()与unescape(). decodeURI()与encodeURI(); JavaScript escape() 函数 定义和使用方法 ...
- hdu5389 Zero Escape DP+滚动数组 多校联合第八场
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- linux程序设计——多线程(第十二章)
12.8 多线程 之前,总是让程序的主线程只创建一个线程.这节将演示怎样在同一个程序中创建多个线程,然后怎样以不同于其启动顺序将它们合并在一起.此外,还演示多线程编程时easy出现的时序问题. ...
- Spark部分:几个重要的端口汇总
50070:HDFSwebUI的端口号 8485:journalnode默认的端口号 9000:非高可用访问数rpc端口 8020:高可用访问数据rpc 8088:yarn的webUI的端口号 808 ...
- session理解
Session,底层的实现就是一个Map<集合>,有些Data在Server内存中,APP要分层.Data在各个层之间肯定要以一种形态传递(泛型),之前Servlet dao.getLis ...
- Getting started with ASP.NET Core MVC and Visual Studio
This tutorial will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio ...
- 我的spark python 决策树实例
from numpy import array from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree im ...
- 框架:Rureka
ylbtech-框架:Rureka Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.S ...