-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleSieve.java
48 lines (40 loc) · 1.02 KB
/
SimpleSieve.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
import java.util.Scanner;
public class SimpleSieve
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number to find number of prime numbers inside range: ");
int n = sc.nextInt();
int prime[] = new int[n-1];
for(int i = 0;i<n-1;i++)
{
prime[i] = i+2;
}
int temp = 0;
for(int i = 0;i<n-1;i++)
{
for(int j = 0;j <n/2;j++)
{
if(prime[i]!=0)
{
temp = prime[i]*prime[i] + j * prime[i];
if(temp<=n)
prime[temp-2] = 0;
else
continue;
}
else
continue;
}
}
for(int i = 0;i<n-1;i++)
{
if(prime[i]>0)
System.out.print(prime[i] + " ");
else
continue;
}
sc.close();
}
}