第十三届JAVAA组决赛——3.选素数
import java.io.*;
import java.util.*;
public class Main{
public static Scanner sc = new Scanner(System.in);
public static int n;
public static boolean isPrimes(int n) {
for(int i = 2; i <= n / i; i++)
if(n % i == 0) return false;
return true;
}
public static int getMaxPrimes(int n) {
int res = 0;
int i = 2;
while(n > 1) {
if(n % i == 0) {
res = Math.max(res, i);
while(n % i == 0) n /= i;
}else i++;
}
return res;
}
public static void main(String[] args) {
n = sc.nextInt();
if(isPrimes(n)) {
System.out.println("-1");
System.exit(0);
}
int pos = getMaxPrimes(n);
int res = Integer.MAX_VALUE;
for(int m = n - pos + 1; m <= n; m++) {
if(isPrimes(m)) continue;
int p = getMaxPrimes(m);
int x = m - p + 1;
res = Math.min(res, x);
}
System.out.println(res);
}
}