**P1304 哥德巴赫猜想 ---素数筛
**来源:洛谷
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int n;
bool f[N],f1[N];
struct node
{
int x,y,z;
}a[N];
bool cmp(node x1,node x2)
{
return x1.z<x2.z;
}
int len=0;
int primes[N],cnt=0;
int main()
{
cin>>n;
f[0]=1;
f[1]=1;
// 线性筛,求出所有素数
for(int i=2;i<=n;i++)
{
if(!f[i]) primes[++cnt]=i;
for(int j=1;i*primes[j]<=n;j++)
{
f[i*primes[j]]=1;
if(i%primes[j]==0) break;
}
}
int now;
for(int i=1;2*primes[i]<=n;i++)
{
for(int j=i;j<=cnt;j++)
{
now=primes[i]+primes[j];
if(now>=4&&now<=n&&now%2==0&&f1[now]==0)
{
f1[now]=1;
a[++len].z=now;
a[len].x=primes[i];
a[len].y=primes[j];
}
}
}
sort(a+1,a+1+len,cmp);
for(int i=1;i<=len;i++)
{
cout<< a[i].z<< "="<< a[i].x<< "+"<<a[i].y<<endl;
}
return 0;
}
P1304 哥德巴赫猜想 ---素数筛
最新推荐文章于 2025-05-21 12:04:25 发布