-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlmostPrime.java
More file actions
31 lines (29 loc) · 802 Bytes
/
Copy pathAlmostPrime.java
File metadata and controls
31 lines (29 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
public class AlmostPrime {
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int almostPrime = 0;
for (int i = 1; i <= n; i++) {
int prime = 0;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
if (isPrime(j)) prime++;
}
}
if (prime == 2) {
almostPrime++;
}
}
System.out.println(almostPrime);
}
}