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. static int max=100000;
  11. static boolean sieve[]=new boolean[max+1];
  12. static void createsieve()
  13. {
  14. Arrays.fill(sieve,true);
  15. sieve[1]=false;
  16. sieve[0]=false;
  17.  
  18. for(int i=2;i<=Math.sqrt(max);i++)
  19. {
  20. if(sieve[i])
  21. {
  22. for(int j=i*i;j<=max;j+=i)
  23. sieve[j]=false;
  24. }
  25. }
  26. }
  27.  
  28. public static void main (String[] args) throws java.lang.Exception
  29. {
  30. // your code goes here
  31. createsieve();
  32. Scanner sc=new Scanner(System.in);
  33. int n=sc.nextInt();
  34. List<Integer> li=new ArrayList<>();
  35. for(int i=1;i<=n;i++)
  36. {
  37. if(sieve[i])
  38. li.add(i);
  39. }
  40.  
  41. System.out.println(li);
  42.  
  43. }
  44. }
Success #stdin #stdout 0.16s 57040KB
stdin
5
stdout
[2, 3, 5]