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 + " "); + } + } + } + +}