-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountPrimes.java
93 lines (76 loc) · 1.94 KB
/
CountPrimes.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package math.medium;
/***
* Problem 204 in Leetcode: https://leetcode.com/problems/count-primes/
*
* Given an integer n, return the number of prime numbers that are strictly less than n.
*
* Example 1:
* Input: n = 10
* Output: 4
*
* Example 2:
* Input: n = 5000000
* Output: 348513
*
* Example 3:
* Input: n = 1
* Output: 0
*/
public class CountPrimes {
private static int generated;
private static final int N = (int) 5e6 + 2;
private static final int[] primes = new int[N];
public static void main(String[] args) {
int num = 5000000;
System.out.println("Brute Force: " + getCountOfPrimesBruteForce(num));
System.out.println("Sieve: " + getCountOfPrimesSieve(num));
}
private static int getCountOfPrimesBruteForce(int num) {
int count = 0;
for (int i = 2; i < num; i++) {
if (isPrimes(i)) {
count++;
}
}
return count;
}
private static boolean isPrimes(int num) {
if (num <= 1) {
return false;
}
if ((num == 2) || (num == 3)) {
return true;
}
if ((num % 2 == 0) || (num % 3 == 0)) {
return false;
}
for (int i = 5; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
private static int getCountOfPrimesSieve(int num) {
if (generated == 0) {
generatePrimes();
}
int count = 0;
for (int i = 2; i < num; i++) {
if (primes[i] == 0) {
count++;
}
}
return count;
}
private static void generatePrimes() {
for (int i = 2; i * i < N; i++) {
if (primes[i] == 0) {
for (int j = i * i; j < N; j += i) {
primes[j] = 1;
}
}
}
generated = 1;
}
}