-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEratosthenes.java
More file actions
40 lines (38 loc) · 1.06 KB
/
Copy pathEratosthenes.java
File metadata and controls
40 lines (38 loc) · 1.06 KB
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
package algorithm.module;
public class Eratosthenes {
public int[] solve(int n) {
boolean[] isPrime = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
int count = 0;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) count++;
}
int[] primes = new int[count];
for (int i = 2, j = 0; i <= n; i++) {
if (isPrime[i]) primes[j++] = i;
}
return primes;
}
public int[] solve2(int n) {
int[] arr = new int[n];
arr[1] = 1;
if (n < 2) return arr;
for (int i = 2; i < n; i++) {
if (arr[i] == 1) {
for (int j = 2; i * j < n; j++) {
arr[i * j] = 1;
}
}
}
return arr;
}
}