From 83bc552894a49dd0d61ff62e8f3022c010dc1dc5 Mon Sep 17 00:00:00 2001 From: Avanish Dubey <67569753+avanish460@users.noreply.github.com> Date: Fri, 29 Oct 2021 19:24:01 +0530 Subject: [PATCH] Create SieveOfElatorSthenesAlgorithm --- SieveOfElatorSthenesAlgorithm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SieveOfElatorSthenesAlgorithm diff --git a/SieveOfElatorSthenesAlgorithm b/SieveOfElatorSthenesAlgorithm new file mode 100644 index 0000000000..f6504253b8 --- /dev/null +++ b/SieveOfElatorSthenesAlgorithm @@ -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; + } + +}