1.

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Angewandte Mathematik, SS11
* Problem: 10394 Twin Primes
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=15&page=show_problem&problem=1335
*
* @author Unverzart Michael
* @author Wurth Manuel
* @version 1.0, 3/4/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.392
*/

public class Main {

public static void main(String[] args) throws Exception{
//Sieb des Eratosthenes
//false = prime
//true = no prime (gestrichen)
int N = 18409201;
int M = 4291; //sqrt(18409201)
boolean sieb[] = new boolean[N+1];
for(int j = 4;j<N;j+=2){
sieb[j]=true;
}
for(int i = 3; i<M;i+=2){
if(!sieb[i]){
for(int j = i*i;j<N;j+=i){
sieb[j]=true;
}
}
}
//Twin Primes aus Sieb ermitteln
int O = 100000;
String[] twin = new String[O+1];
int i = 4;
int r = 1;
twin[r]= String.format("(%d, %d)",i-1, i+1);
for (i = 6; i < N; i+=6) {
if(!sieb[i-1] && !sieb[i+1]) twin[++r]= String.format("(%d, %d)",i-1, i+1);
}
//Ein-/Ausgabe
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while((inputLine = reader.readLine()) != null){
int n = new Integer(inputLine);
System.out.println(twin[n]);
}
}
}


-----------------------------------------------------

1.

package acm_10394_twin_primes;

import java.util.Scanner;

/**
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: acm_10394_twin_primes
* Link:
*
* @author Martin Lambeck
* @version 1.0, 14.09.2010
*
* Method : sieve, bitfield
* Status : Accepted
* Runtime: 1.656
*/


public class Main
{
static final int intbits = 32;
static final int MAX = 20000000;
static int[] composed = new int[MAX/intbits + 1]; //bitfield 0 = prime, 1 = composed (wrong values for 0 and 1)
static Scanner sc = new Scanner(System.in);
static int[] highTwin = new int[100001];

public static void main(String... args)
{
init();
init2();

while (sc.hasNextInt())
testcase();
}

static void init2()
{
boolean prevPrime = true;
int ind, ofs, cp;
int pair = 0;

for (int i = 5; (i <= MAX) && (pair <= 100000); i += 2)
{
ind = i / intbits;
ofs = i % intbits;
cp = (composed[ind] >> ofs) & 1;

if (cp == 0)
if (prevPrime)
{
highTwin[pair] = i;
pair++;
}

prevPrime = (cp == 0);
}
}

public static void init()
{
int sqrtMax = (int)Math.sqrt(MAX);
int ind;
int ofs;
int cp;

for (int i = 2; i <= sqrtMax; i++)
{
ind = i / intbits;
ofs = i % intbits;
cp = (composed[ind] >> ofs) & 1;

if (cp == 0) //if i is prime
{
for (int j = i*i; j <= MAX; j += i) //all multiples of i are composed
{
ind = j / intbits;
ofs = j % intbits;
composed[ind] |= 1 << ofs; //j is composed
}
}
}
}

public static void testcase()
{
int n = sc.nextInt();
int ht = highTwin[n-1];

System.out.printf("(%d, %d)%n", ht-2, ht);
}
}


2.

/**
* FWP: Ausgewaehlte Probleme aus dem ACM (SS10)
*
* Method: number theory
* Problem: 10394 - Twin Primes
* Accepted: 0.972
* @author Evgeni Pavlidis
*
*/

import java.io.*;
import java.util.*;

class Main {

private static int MAX = 20000000;
private static int HIGH = 312500;

private static long FULL = 0xFFFFFFFFFFFFFFFFl;
private static long MASK = 0x000000000000003Fl;

// Bitmap of numbers.
// isPrime[0] holds bitwise info for the numbers 0 - 63
// isPrime[1] for 64 - 127 and so on
private static long isPrime[] = new long[HIGH+1];
private static int twinPrime[] = new int[1000001];


private static void sievePrimes()
{
for(int i = 0; i <= HIGH; i++)
isPrime[i] = FULL;

for(int i = 4; i <= MAX; i+=2)
isPrime[i >> 6] &= ~(1l << (i & MASK));

for(int i = 3; i <= Math.sqrt(MAX); i+=2)
if((isPrime[i >> 6] & (1l << (i & MASK))) != 0)
for(int j = i+i; j <= MAX; j+=i)
isPrime[j >> 6] &= ~(1l << (j & MASK)) ;
}

private static void findTwinPrimes()
{
int count = 1;
for(int i = 3; i < MAX-2; i+=2)
if( ( (isPrime[ i >> 6] & (1l << ( i & MASK))) != 0) &&
( (isPrime[(i+2) >> 6] & (1l << ((i+2) & MASK))) != 0) )
twinPrime[count++] = i;
}

public static void main(String...args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

sievePrimes();
findTwinPrimes();

String input;
int n;
while( (input = reader.readLine()) != null)
{
n = Integer.parseInt(input);
System.out.println("(" + twinPrime[n] + ", " + (twinPrime[n]+2)+ ")");
}
}
}