素数(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],找 ...
随机推荐
- Spring学习总结(2)——Spring的常用注解
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). ? 1 <context:compon ...
- (15)Spring Boot使用Druid和监控配置【从零开始学Spring Boot】
Spring Boot 系列博客] 更多查看博客:http://412887952-qq-com.iteye.com/blog Spring Boot默认的数据源是:org.apache.tomcat ...
- Thread interrupt表示什么
Thread interrupt表示什么 学习了:https://www.zhihu.com/question/41048032?sort=created 学习了:http://blog.csdn.n ...
- POJ3126——Prime Path
非常水的一道广搜题(专业刷水题). .. #include<iostream> #include<cstdio> #include<queue> #include& ...
- maven创建web报错failure to transfer org.codehaus.plexus
failure to transfer org.codehaus.plexus:plexus:pom:2.0.5 from http:// repo.maven.apache.org/maven2 w ...
- wpf Command canExecute 更新
可以调用以下语句通知 CommandManager.InvalidateRequerySuggested();
- P1025小飞侠的游园方案
描述 经过抽签选择,小智将军第一个进入考场. 菜虫:(身上散射出华贵(?)的光芒)欢迎你,第一位挑战者!! 小智:……(走到菜虫身后,关灯)女王陛下,虽然我们国家现在很富裕,但也请您不要浪费电来用这么 ...
- 【NOIP 2011】 计算系数
[题目链接] https://www.luogu.org/problemnew/show/P1313 [算法] 二项式定理 [代码] #include<bits/stdc++.h> usi ...
- php mktime和strtotime
本文章来给各位同学介绍一下利用php用strtotime或mktime指定日期数据(本周,上周,本月,上月,本季度)实例,希望对各位同学会有所帮助呀. strtotime定义和用法 strtotime ...
- A - A Compatible Pair
Problem description Nian is a monster which lives deep in the oceans. Once a year, it shows up on th ...