Advertisement
Spocoman

04. Sieve of Eratosthenes

Oct 23rd, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         int[] numbers = new int[n];
  9.         Arrays.setAll(numbers, p -> p >= n ? 0 : p + 1);
  10.  
  11.         for (int number : numbers) {
  12.             boolean isPrime = number > 1;
  13.             for (int i = 2; i <= Math.sqrt(number); i++) {
  14.                 if (number % i == 0) {
  15.                     isPrime = false;
  16.                     break;
  17.                 }
  18.             }
  19.             if (isPrime) {
  20.                 System.out.print(number + " ");
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement