SGU231-Prime Sum 还是素数

231. Prime Sum
time limit per test: 0.5 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
 
 
 
 
 
 
题目说的是,求两个素数的和还为素数的对数,并把符合条件的写下来。

因为大于2的素数都为奇数,所以两奇数和为偶数一定不是素数。剩下的就只有2和其他素数相加。

这样就转换为有多少个差为2的素数。

 

 

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
bool vis[1000007];
void isprime(int n)
{
    int k=sqrt(n+0.5);
    for(int i=2; i<=k; i++)
    {
        if(!vis[i])
            for(int j=i*i; j<=n; j+=i)
                vis[j]=1;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        vis[0]=1,vis[1]=1;
        int count=0;
        isprime(n);
        for(int i=2; i<=n-2; i++)
        {
            if(!vis[i]&&!vis[i+2]&&(i+2)<=n)
                count++;
        }
        printf("%d\n",count);
        if(count)
        {
            for(int i=2; i<=n-2; i++)
            {
                 if(!vis[i]&&!vis[i+2]&&(i+2)<=n)
                    printf("2 %d\n",i);
            }
        }
    }
    return 0;
}


 

 

 

 

 











Server time: 2013-12-05 16:13:31Online Contester Team © 2002 - 2013. All rights reserved.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值