forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime.java
More file actions
38 lines (29 loc) · 671 Bytes
/
Prime.java
File metadata and controls
38 lines (29 loc) · 671 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
32
33
34
35
36
37
38
package Math;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 27/09/18
* Time - 11:35 AM
*/
public class Prime {
public ArrayList<Integer> sieve(int A) {
ArrayList<Integer> ans = new ArrayList<>();
boolean prime[] = new boolean[A+1];
for(int i=0;i<=A;i++){
prime[i] = true;
}
for(int p=2; p*p<= A; p++){
if(prime[p]){
for(int j=2*p;j<=A;j+=p){
prime[j] = false;
}
}
}
for(int i=2;i<=A;i++){
if(prime[i]){
ans.add(i);
}
}
return ans;
}
}