-
Notifications
You must be signed in to change notification settings - Fork 0
/
cntprimenos12(2).java
49 lines (41 loc) · 1.05 KB
/
cntprimenos12(2).java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Arrays;
import java.util.Vector;
class PrimeCount
{
static int primeCount(int arr[], int n)
{
int max_val = Arrays.stream(arr).max().getAsInt();
Boolean[] prime = new Boolean[max_val + 1];
for (int i = 0; i < max_val + 1; i++)
{
prime[i] = true;
}
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= max_val; p++)
{
if (prime[p] == true)
{
for (int i = p * 2; i <= max_val; i += p)
{
prime[i] = false;
}
}
}
int count = 0;
for (int i = 0; i < n; i++)
{
if (prime[arr[i]])
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = {1,2,3,5,7,11,14,23};
int n = arr.length;
System.out.println(primeCount(arr, n));
}
}