UVA Twin Primes

10394 - Twin Primes

Time limit: 3.000 seconds 


Twin Primes
Input: standard input
Output: standard output
Time Limit: 30 seconds

 

Twin primes are pairs of primes of the form (p, p+2). The term "twin prime" was coined by Paul Stäckel (1892-1919). The first few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43). In this problem you are asked to find out the S-th twin prime pair where S is an integer that will be given in the input.

 

Input

The input will contain less than 10001 lines of input. Each line contains an integers S (1<=S<=100000), which is the serial number of a twin prime pair. Input file is terminated by end of file. 

 

Output

For each line of input you will have to produce one line of output which contains the S-th twin prime pair. The pair is printed in the form (p1,<space>p2). Here <space> means the space character (ASCII 32). You can safely assume that the primes in the 100000-th twin prime pair are less than 20000000.

 

Sample Input
1

2

3

4

 

Sample Output
(3, 5)

(5, 7)

(11, 13)

(17, 19)


代码:


#include <stdio.h>
#include <math.h>
#include <string.h>
#define maxn 20000010
int a[maxn]={1,1,0},b[100010];
int main()
{
    int i,j,sum;
    for(i=2;i<sqrt(maxn);i++)
    {
        if(a[i]==0)
        {
            for(j=i+i;j<=maxn;j+=i)
                a[j]=1;
        }
    }
    sum=1;j=3;
    while(1)
    {
        if(a[j]==a[j+2]&&a[j]==0)
        {
            b[sum]=j;sum++;j=j+2;
        }
        else j++;
        if(sum==100001) break;
    }

    int n;
    while(scanf("%d",&n)!=EOF)
    {
        printf("(%d, %d)\n",b[n],b[n]+2);
    }
    return 0;
}

奉献水题代码一道,因为把这道水题推荐给队友,害队友超时还被队友骂~::>_<:: ..........

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值