- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEratosthenes.java
More file actions
Latest commit
26 lines (23 loc) · 677 Bytes
/
Copy pathEratosthenes.java
File metadata and controls
26 lines (23 loc) · 677 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
importjava.util.Arrays;
publicclassEratosthenes {
privatestaticfinalintMAX = 30;
publicstaticvoidmain(String[] args) {
boolean [] flag = newboolean[MAX];
Arrays.fill(flag, true);
eratosthenes(flag, MAX);
for (inti = 2; i < flag.length; i++){
if (flag[i]){
System.out.println(i + " is prime number");
}
}
}
publicstaticvoideratosthenes(boolean[] flag, intk){
for (inti = 2; i*i < k ; i++) {
if (flag[i]){
for (intj = i*i; j < k ; j+=i) {
flag[j] = false;
}
}
}
}
}