From b5713ccf3c2d2fc438c5e1ef57faa98d6ad40f67 Mon Sep 17 00:00:00 2001 From: Prince Bharti Date: Sun, 9 Oct 2022 00:02:50 +0530 Subject: [PATCH] Seive Of Eratosthenes --- DataStructures/SieveOfEratosthenes.java | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 DataStructures/SieveOfEratosthenes.java diff --git a/DataStructures/SieveOfEratosthenes.java b/DataStructures/SieveOfEratosthenes.java new file mode 100644 index 0000000..422210a --- /dev/null +++ b/DataStructures/SieveOfEratosthenes.java @@ -0,0 +1,34 @@ +public class SieveOfEratosthenes { + + public static void main(String[] args) { + // TODO Auto-generated method stub + int n = 25; + SOE(n); + + } + public static void SOE(int n) + { + boolean[] multiple = new boolean[n+1]; + for(int i = 2 ; i <= n ; i++) { + multiple[i] = true; + } + for(int factor = 2; factor*factor <= n; factor++) + { + if(multiple[factor]) { + for(int mult = 2; mult*factor<=n;mult++) + { + multiple[mult*factor] = false; + + } + } + + } + for(int i = 0 ; i <= n ; i++) + { + if(multiple[i]) { + System.out.print(i + " "); + } + } + } + +}