fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int N = sc.nextInt();
  7.  
  8. for (int i = 2; i <= N; i++) {
  9. if (isPrime(i)) {
  10. System.out.print(i + " ");
  11. }
  12. }
  13. }
  14.  
  15. private static boolean isPrime(int num) {
  16. if (num < 2) return false;
  17. for (int i = 2; i * i <= num; i++) {
  18. if (num % i == 0) return false;
  19. }
  20. return true;
  21. }
  22. }
  23.  
Success #stdin #stdout 0.21s 56908KB
stdin
10
stdout
2 3 5 7