fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. public static void main (String[]args){
  6.  
  7. Scanner read = new Scanner(System.in);
  8. long []Primes=new long [5000001];
  9. int sum =0,i=0,c;
  10. int t = Integer.parseInt(read.nextLine());
  11. boolean flag = true;
  12. long start, end,tester,n,counter=1;
  13. double root;
  14. String string;
  15.  
  16. while (counter<=t){
  17. string = read.nextLine();
  18. if ("".equals(string)){
  19. break;
  20. }
  21. n = Long.parseLong(string);
  22. if(n>=1 && n<= 5000000){
  23. Primes[0] = 2;
  24. sum=0;
  25. for (c=2,i=0;i!=n;c++,flag=true){
  26. root = (int)Math.sqrt(c);
  27. for(int q=0;q<=i&&Primes[q]<=root;q++){
  28. tester = c % Primes[q];
  29. if (tester == 0){
  30. flag = false;
  31. break;
  32. }
  33. }
  34. if (flag == true){
  35. Primes[i+1] = c;
  36. i++;
  37. }
  38. }
  39. if (i == n){
  40. System.out.println(Primes[i]);
  41. }
  42. counter++;
  43. }
  44. }
  45. }
  46. }
Time limit exceeded #stdin #stdout 5s 212864KB
stdin
8
1
10
100
1000
10000
100000
1000000
stdout
2
29
541
7919
104729
1299709