From dc76fa57fa147da8e1a8fe730bb6a68002a9f141 Mon Sep 17 00:00:00 2001 From: Sarfaraz Alam <56781879+sarfarazalam13@users.noreply.github.com> Date: Wed, 4 Oct 2023 02:02:41 +0530 Subject: [PATCH] Create SieveofEratosthenes.java --- SieveofEratosthenes.java | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 SieveofEratosthenes.java diff --git a/SieveofEratosthenes.java b/SieveofEratosthenes.java new file mode 100644 index 0000000..c1ed35c --- /dev/null +++ b/SieveofEratosthenes.java @@ -0,0 +1,50 @@ +/*optimal way to find the primes +Explanation +Initially we mark all the values of the array as true and then we go on modifying the array boolean values by marking the multiples as false suppose the pointer is on 2 then we mark all the numbers which are multiple of 2 as false starting from the square of 2 ie 4,4+2=6,6+2=8…in this way only primes are left in the end + +Let us take an example when n = 50. So we need to print all prime numbers smaller than or equal to 50. +We create a list of all numbers from 2 to 50. + +According to the algorithm we will mark all the numbers which are divisible by 2 and are greater than or equal the square of it. + +Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3 and are greater than or equal to the square of it. + +We move to our next unmarked number 5 and mark all multiples of 5 and are greater than or equal to the square of it. + +We continue this process and our final table will look like below: + +So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47.*/ + +import java.util.Scanner; +public class SeiveofErathobess { + static void primes(int n) + { + boolean p[]=new boolean[n+1]; + for(int i=0;i