Ultraviolet A 10394 Twin Primes

Source: Internet
Author: User

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 outS-Th twin prime pair whereSIs an integer that will be given in the input.

 

Input

The input will contain in less10001Lines of input. Each line contains an integersS (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 containsS-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 in100000-thTwin prime pair are less20000000.

 

Sample Input
1

2

3

4

 

Sample Output
(3, 5)

(5, 7)

(11, 13)

(17, 19)

Enter n to output the n pair of twin prime numbers. Twin Prime Number definition: If I and I + 2 are both prime numbers, I and I + 2 are a pair of twin prime numbers. The problem focuses on determining prime numbers and twin prime numbers. If you use a common method to determine the prime number, it will certainly time out. Therefore, you need to use a quick method to determine the prime number. Here we use the limit method. The 'distinct' method is an efficient method for determining prime numbers. It can filter out prime numbers in a certain interval at a time. The essence of its algorithm principle is to make full use of the definition of prime numbers, that is, the approximate number of prime numbers is only 1 and itself. If m is a multiple of n, m is definitely not a prime number. So we only need to screen out all multiples of known prime numbers in a certain range, and the rest is definitely prime numbers. Because only it is not a multiple of other numbers (except for 1 and itself ).

The specific method is to first arrange N natural numbers in order. 1 is not a prime number or a combination. The second number 2 is the prime number, and all the numbers that can be divisible by 2 after 2 are excluded. The first undivided number behind 2 is 3, leaving 3, and then dividing all the numbers behind 3 that can be divisible by 3. 3. The first unallocated number after the end is 5, leaving 5, and then dividing all the numbers that can be divisible by 5. In this way, all the groups not greater than N will be screened out, leaving all the prime numbers not greater than N.


# Include <stdio. h> # include <math. h> # define N 20000000 + 5int a [100005], prime [N]; void deal ()/* differentiate prime numbers and operators */{int I, j; for (I = 2; I <= N; I ++) prime [I] = 1; for (I = 2; I <= sqrt (N); I ++) {if (prime [I]) for (j = I + I; j <= N; j + = I) prime [j] = 0 ;}} void judge () /* determine the twins */{int I, j; for (I = 3, j = 1; I + = 2) {if (prime [I] = prime [I + 2] & prime [I] = 1) a [j ++] = I; if (j> 100000) break ;}} int main () {int n; deal (); judge (); while (~ Scanf ("% d", & n) printf ("(% d, % d) \ n", a [n], a [n] + 2 ); return 0 ;}

 

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.