筛法
来源:互联网 发布:阿里云投资了多少钱 编辑:程序博客网 时间:2024/06/05 16:51
描述:
用筛法求[a,b]中的素数。
Find out the prime numbers in [a, b].
输入:
2个正整数:a b。
a、b均在100000000以内,且a小于等于b,且b-a<250000。
2 positive integers: a, b. Both a and b are less than or equal 100000000 and a is less than or equal to b, and b - a < 250000.
输出:
[a b]区间内的所有素数,每个单独一行。
All primes in [a, b], each one in a row.
输入样例:
2 5
输出样例:
2
3
5
#include<iostream>
#include<math.h>
using namespace std;
int c[100001];
int d[250001];
int a,b;
void fuzhi();
void shaifa();
void fuzhi2();
void shaifa2();
void output();
int main()
{
cin>>a>>b;
fuzhi();
shaifa();
fuzhi2();
shaifa2();
output();
return 0;
}
void fuzhi()
{
int i;
for(i=0;i<=sqrt(b);i++)
c[i]=1;
}
void shaifa()
{
c[0]=0;
c[1]=0;
int i,j;
for(i=2;i<=sqrt(b);i++)
{
if(c[i]==1)
for(j=2;i*j<=sqrt(b);j++)
{
c[i*j]=0;
}
}
}
void fuzhi2()
{
int i;
for(i=0;i<=b-a;i++)
d[i]=1;
}
void shaifa2()
{
int i,k,l;
for(i=2;i<a;i++)
if(a%i==0)
{
d[0]=0;
break;
}
for(k=2;k<=sqrt(b);k++)
{
if(c[k]==1)
{
l=(a/k)*k+k-a;
for(;l<=b-a;l=l+k)
d[l]=0;
}
}
for(i=a;i<=sqrt(b);i++)
if(c[i]==1)
{
d[i-a]=1;
}
if(a==1)
d[0]=0;
}
void output()
{
int i;
for(i=0;i<=b-a;i++)
if(d[i]==1)
cout<<a+i<<endl;
}
- 筛法
- 筛法
- 筛法
- 筛法
- 筛法
- 筛法,埃氏筛法 Eular筛法
- Eratosthenes筛法
- POJ3292 筛法
- Eratosthenes筛法
- Eratosthenes筛法
- Eratosthenes筛法
- Eratosthenes筛法
- 【1453】筛法
- 筛法2
- Eratosthenes筛法
- Eratosthenes筛法,求素数
- Eratosthenes筛法--Haskell实现
- 筛法 简单演示程序
- python学习(3)anaconda的介绍 以及 Spyder 的 IPython 的%魔术命令
- HTML基础 超链接基础2
- 使用sysbench对Mysql进行压力测试查询性能
- JavaWeb学习心得之JDBC处理MySQL大数据
- 谈笑风生线段树(区间修改)
- 筛法
- 关于dubbox-2.8.4血与泪的总结
- 就算萌新也能看得懂的gulp教程(1):①读文件②修改③写到新文件
- python _foo __foo
- CCPC2017秦皇岛赛区游记
- CentOS7 安装配置rsync-3.0.9
- SSM综合项目实战(TTSC) -- day13 订单、定时器Quartz
- 【练习】二叉树的实现
- setlocale函数和wchar_t笔记