sgu 231. Prime Sum 素数筛选


231. Prime Sum
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard



Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N.

Input
The input of the problem consists of the only integer N (1<=N<=10^6).

Output
On the first line of the output file write the number of pairs meeting the requirements. Then output all pairs one per line (two primes separated by a space).

Sample test(s)

Input
 
4
Output
 
  
0



素数只有2是偶数,其他两奇数之和为偶数不是素数,所以就是求素数差为2的素数对数量。


#include <bits/stdc++.h>

using namespace std;

bool v[1000005];

void solve(){
    int n;
    scanf("%d",&n);
    for(int i=2;i*i<=n;i++){
        if(v[i]==1)continue;
        for(int j=2*i;j<=n;j+=i){
            v[j]=1;
        }
    }

    int ans=0;
    for(int i=5;i<=n;i++){
        if(v[i]==0&&v[i-2]==0){
            ans++;
        }
    }
    printf("%d\n",ans);
    for(int i=5;i<=n;i++){
        if(v[i]==0&&v[i-2]==0){
            printf("%d %d\n",2,i-2);
        }
    }

}

int main(){
    solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值