Advertisement
CR7CR7

isPrime

Aug 9th, 2023
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class BiggestPrimeSecond {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         Boolean[] isPrime = new Boolean[n + 1];
  11.         Arrays.fill(isPrime, true);
  12.  
  13.         for (int i = 2; i < Math.sqrt(n) + 1; i++) {
  14.             if (isPrime[i]) {
  15.                 for (int j = i * i; j <= n; j += i) {
  16.                     isPrime[j] = false;
  17.                 }
  18.             }
  19.         }
  20.  
  21.         for (int i = isPrime.length - 1; i > 0 ; i--) {
  22.             if (isPrime[i]) { // not (!isPrime[i])
  23.                 System.out.println(i);
  24.                 return;
  25.             }
  26.         }
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement