-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha14sieveAlgo.java
More file actions
33 lines (31 loc) · 1.08 KB
/
Copy patha14sieveAlgo.java
File metadata and controls
33 lines (31 loc) · 1.08 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
// sieve algorithm to find prime no from 1 to n.
import java.util.Arrays;
import java.util.Scanner;
public class a14sieveAlgo {
public static void sieveAlgo(int num){
// if array size is 40 then we can store only 0-39 elements that's y we take arr size num+1.
boolean arr[] = new boolean[num+1];
Arrays.fill(arr, true);
int counter;
// 1 is neither prime nor composite no.
for(counter = 2; counter<=num; counter++){
if (arr[counter]==true) {
for(int factor = counter+counter; factor<=num; factor+=counter){
arr[factor]=false;
}
}
}
for(int i=2; i<=num; i++){
if (arr[i]==true) {
// System.out.println(i+" "+arr[i]);
System.out.println(i+" is a prime no.");
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter limit: ");
int num = sc.nextInt();
sieveAlgo(num);
}
}