Skip to content

Commit

Permalink
Merge pull request #1263 from avanish460/patch-1
Browse files Browse the repository at this point in the history
Create SieveOfElatorSthenesAlgorithm
  • Loading branch information
fineanmol authored Oct 29, 2021
2 parents 20eb689 + 83bc552 commit c6f717e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions SieveOfElatorSthenesAlgorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.Arrays;
import java.util.Scanner;

public class SieveOfEratoSthenes {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean isprime[] = sieveoferatosthenes(n);
for(int i=0; i<=n; i++) {
System.out.println(i+" "+isprime[i]);
}

}

static boolean[] sieveoferatosthenes(int n) {
boolean isprime[] = new boolean[n+1];
Arrays.fill(isprime,true);
isprime[0] = false;
isprime[1] = false;

for(int i=2; i*i <= n; i++) {
for(int j = 2*i; j <=n; j +=i) {
isprime[j] = false;
}
}
return isprime;
}

}

0 comments on commit c6f717e

Please sign in to comment.