@@ -0,0 +1,2338 @@
+import java.util.*;%0Aimport static java.lang.Math.*;%0Aimport static java.lang.System.*;%0Aimport java.util.stream.*;%0A%0A/*%0AProblem name: 10140 Prime Distance%0AProblem url: https://uva.onlinejudge.org/external/101/10140.pdf%0AAuthor: Andrey Yemelyanov%0A*/%0Apublic class _10140 %7B%0A  public static void main(String%5B%5D args)%7B%0A    Scanner s = new Scanner(in);%0A    List%3CInteger%3E primes = sieve(BOUND);%0A    while(s.hasNext())%7B%0A      int L = s.nextInt(); int U = s.nextInt();%0A      int%5B%5D mostDistant = mostDistantPair(L, U, primes);%0A      int%5B%5D closest = closestPair(L, U, primes);%0A      if(closest == null %7C%7C mostDistant == null) System.out.println(%22There are no adjacent primes.%22);%0A      else System.out.printf(%22%25d,%25d are closest, %25d,%25d are most distant.%5Cn%22,%0A        closest%5B0%5D, closest%5B1%5D,%0A        mostDistant%5B0%5D, mostDistant%5B1%5D);%0A    %7D%0A  %7D%0A%0A  static int%5B%5D mostDistantPair(int L, int U, List%3CInteger%3E primes)%7B%0A    long dist = Integer.MIN_VALUE;%0A    int%5B%5D pair = null; long prevPrime = -1;%0A    for(long i = L; i %3C= U; i++)%7B%0A      if(isPrime(i, primes))%7B%0A        if(prevPrime != -1 && i - prevPrime %3E dist)%7B%0A          dist = i - prevPrime;%0A          pair = new int%5B%5D%7B(int)prevPrime, (int)i%7D;%0A        %7D%0A        prevPrime = i;%0A      %7D%0A    %7D%0A    return pair;%0A  %7D%0A%0A  static int%5B%5D closestPair(int L, int U, List%3CInteger%3E primes)%7B%0A    long dist = Integer.MAX_VALUE;%0A    int%5B%5D pair = null; long prevPrime = -1;%0A    for(long i = L; i %3C= U; i++)%7B%0A      if(isPrime(i, primes))%7B%0A        if(prevPrime != -1 && i - prevPrime %3C dist)%7B%0A          dist = i - prevPrime;%0A          pair = new int%5B%5D%7B(int)prevPrime, (int)i%7D;%0A        %7D%0A        prevPrime = i;%0A      %7D%0A    %7D%0A    return pair;%0A  %7D%0A%0A  static final int BOUND = 1000000;%0A%0A  static boolean isPrime(long n, List%3CInteger%3E primes)%7B%0A    int bound = (int)ceil(sqrt(n));%0A    for(int i = 0; i %3C primes.size() && bound %3E= primes.get(i); i++)%7B%0A      if(n %25 primes.get(i) == 0) return false;%0A    %7D%0A    return true;%0A  %7D%0A%0A  static List%3CInteger%3E sieve(int upperBound)%7B%0A    boolean%5B%5D sieve = new boolean%5BupperBound + 1%5D;%0A    List%3CInteger%3E primes = new ArrayList%3C%3E();%0A    for(int i = 2; i %3C sieve.length; i++) sieve%5Bi%5D = true;%0A    for(int i = 2; i %3C sieve.length; i++)%7B%0A      if(sieve%5Bi%5D)%7B%0A        primes.add(i);%0A        for(long j = (long)i * i; j %3C sieve.length; j += i) sieve%5B(int)j%5D = false;%0A      %7D%0A    %7D%0A    return primes;%0A  %7D%0A%7D%0A

work in progres