UVa 10394 Twin Primes (twin primes)

Source: Internet
Author: User
Tags time limit

10394-twin Primes

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1335

Twin primes are pairs of primes of the form (p, p+2). The term "twin prime" was coined by Paul Stckel (1892-1919). The the few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43). In this problem your are asked to find out the s-th twin prime pair Where's is a integer that would be given in the input.

Input

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

Output

For each line of input you'll have to produce one line of output which contains the s-th twin prime. The pair is printed in the form (P1,<SPACE>P2). Here <space> means the "space character" (ASCII 32). You can safely assume this primes in 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)

Water problem.

Complete code:

/*0.342s*/
    
#include <cstdio>  
const int MAXN = 20000000;  
    
BOOL VIS[MAXN];  
int PRIME[MAXN/10], C, twinprime[100005];  
    
/* with I as the benchmark sieve composite
//*o (MAXN) Time complexity */
void Cal_prime ()  
{for  
    (int i = 2; i < MAXN; ++i)  
    {  
        if!vis[ I]) prime[c++] = i;  
        for (int j = 0; J < c && I * Prime[j] < MAXN ++j)  
        {  
            vis[i * prime[j]] = true;  
            if (i% prime[j] = = 0) break  
        ;  
    
}} /* Determine the twin prime number
/inline void judge ()  
{  
    int i, J;  
    for (i = 3, j = 1; J <= 100000 i + + 2)  
        if (!vis[i] &&!vis[i + 2])  
            twinprime[j++] = i;  
}  
    
int main ()  
{  
    cal_prime ();  
    Judge ();  
    int n;  
    while (~SCANF ("%d", &n))  
        printf ("(%d,%d) \ n", Twinprime[n], Twinprime[n] + 2);  
    return 0;  
}

Author Signature: CSDN blog Synapse7

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.