Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&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 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)

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>

using namespace std;

vector<int> twinPrimes;
bitset < 20000001> isPrime;
void sieve(){
    isPrime.set();
    isPrime[0] = isPrime[1] = false;
    for(long long i=2;i < 20000001;i++){
        if(isPrime[i]){
            for(long long j=i*i;j<20000001;j+=i){
                isPrime[j] = false;
            }
            if(isPrime[i-2])
                twinPrimes.push_back(i);
        }
    }
}

int main() {
    sieve();
    int n;
    while(scanf("%d",&n) != EOF){
        printf("(%d, %d)\n",twinPrimes[n-1]-2,twinPrimes[n-1]>;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
2
3
4

Output

x
+
cmd
(3, 5)
(5, 7)
(11, 13)
(17, 19)
Advertisements

Demonstration


UVA Online Judge solution - 10394 - Twin Primes  - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10382-Watering Grass - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 104-Arbitrage - UVA Online Judge solution in C,C++,java