Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class BiggestPrimeSecond {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Boolean[] isPrime = new Boolean[n + 1];
- Arrays.fill(isPrime, true);
- for (int i = 2; i < Math.sqrt(n) + 1; i++) {
- if (isPrime[i]) {
- for (int j = i * i; j <= n; j += i) {
- isPrime[j] = false;
- }
- }
- }
- for (int i = isPrime.length - 1; i > 0 ; i--) {
- if (isPrime[i]) { // not (!isPrime[i])
- System.out.println(i);
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement