-
Notifications
You must be signed in to change notification settings - Fork 54
/
sieve_of_eratosthenes
35 lines (33 loc) · 966 Bytes
/
sieve_of_eratosthenes
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
// This program prints all the prime numbers from 2 to n
import java.io.*;
import java.util.*;
class Solution
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(); //n is the number upto which we need to find the prime numbers.
int arr[]=new int[n+1];
for(int i=0;i<=n;i++) //initialize all the indexes with 1.
{
arr[i]=1;
}
for(int i=2;(i*i)<=n;i++) //start from 2 as 2 is the first prime number.
{
if(arr[i]==1)
{
/* replace the value at all indexes that are multiples of j with 0
ultimately we wiil be left with the numbers that are not multiples of any other number.*/
for(int j=i*2;j<=n;j+=i)
{
arr[j]=0;
}
}
}
for(int i=2;i<=n;i++) // print the prime numbers in the range.
{
if(arr[i]==1)
System.out.println(i);
}
}
}