fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(nthPrime(25));
  13. }
  14.  
  15. public static int nthPrime(int n) {
  16. if (n < 2) return 2;
  17. if (n == 2) return 3;
  18. int limit, root, count = 1;
  19. limit = (int)(n*(Math.log(n) + Math.log(Math.log(n)))) + 3;
  20. root = (int)Math.sqrt(limit) + 1;
  21. limit = (limit-1)/2;
  22. root = root/2 - 1;
  23. boolean[] sieve = new boolean[limit];
  24. for(int i = 0; i < root; ++i) {
  25. if (!sieve[i]) {
  26. ++count;
  27. for(int j = 2*i*(i+3)+3, p = 2*i+3; j < limit; j += p) {
  28. sieve[j] = true;
  29. }
  30. }
  31. }
  32. int p;
  33. for(p = root; count < n; ++p) {
  34. if (!sieve[p]) {
  35. ++count;
  36. }
  37. }
  38. return 2*p+1;
  39. }
  40. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
97