fork download
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.math.BigInteger;
  4.  
  5. class WituaAndMath {
  6.  
  7. public static void main(final String[] args) throws Exception {
  8. int T = Integer.parseInt(br.readLine());
  9. while (T-- > 0) {
  10. final long N = Long.parseLong(br.readLine());
  11. System.out.println(findPrimeLowerThanOrEqual(N));
  12. }
  13. }
  14.  
  15. private static long findPrimeLowerThanOrEqual(final long n) {
  16. long res = n;
  17. while (isPrime(res) == false) {
  18. --res;
  19. }
  20. return res;
  21. }
  22.  
  23. private static boolean isPrime(final long res) {
  24. if ((res & 1) == 0) {
  25. return res == 2;
  26. }
  27. return BigInteger.valueOf(res).isProbablePrime(50);
  28. }
  29.  
  30. }
  31.  
Success #stdin #stdout 0.09s 380352KB
stdin
3
2
3
4
stdout
2
3
3