0% found this document useful (0 votes)
72 views16 pages

Simple Sieve

Uploaded by

falgunijain2812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views16 pages

Simple Sieve

Uploaded by

falgunijain2812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

TEST TIME ON TIME AND SPACE COMPLEXITY

URL:https://forms.gle/YDWLEoCL7apATTAU8

QR CODE:
Simple Sieve
SIMPLE SIEVE

• The Sieve of Eratosthenes is the simplest prime number sieve.


• It is a Prime number algorithm to search all the prime numbers in a
given limit.
• There are several prime number sieves. For example- the Sieve of
Eratosthenes, Sieve of Atkin, Sieve of Sundaram, etc.
• This algorithm filters out the prime number in an iterative approach.
• The filtering process starts with the smallest prime number. A Prime
is a natural number that is greater than 1 and has only two divisors,
viz., 1 and the number itself. The numbers that are not primes are
called composite numbers.
• In the sieve of the Eratosthenes method, a small prime number is
selected first, and all the multiples of it get filtered out. The
process runs on a loop in a given range.
PSEUDOCODE

find primes up to N
For all numbers a: from 2 to sqrt(n)
IF a is unmarked THEN
a is prime
For all multiples of an (a < n)
mark multiples of as a composite
All unmarked numbers are prime!
EXAMPLE

Let’s take the number range from 2 to 10.

After applying the Sieve of Eratosthenes, it will produce the list of prime numbers 2, 3, 5, 7
ALGORITHM

Step 1) Create a list of numbers from 2 to the given range n. We start


with 2 as it is the smallest and first prime number.
Step 2) Select the smallest number on the list, x (initially x equals 2),
traverse through the list, and filter the corresponding composite numbers
by marking all the multiples of the selected numbers.
Step 3) Then choose the next prime or the smallest unmarked number on the
list and repeat step 2.
ALGORITHM

Step 4) Repeat the previous step until the value of x should be lesser
than or equal to the square root of n (x<= )

Step 5) After those four steps, the remaining unmarked numbers would be
all the primes on that given range n.
Representation
EXAMPLE

Find the list of prime numbers from 2 to 25. So, n=25.

Step 1) In the first step, we will take a list of numbers from 2


to 25 as we selected n=25.
Numbers from 2 to 25
Representation EXAMPLE

Step 2) Then we select the smallest number on the list, x. Initially x=2
as it is the smallest prime number. Then we traverse through the list and
mark the multiples of 2.
The multiples of 2 for the given value of n is: 4, 6, 8, 10, 12, 14, 16,
18, 20, 22, 24.
Eliminated multiples of 2

Note: Blue colour denotes the


selected number, and pink
colour denotes the eliminated
multiples
Representation EXAMPLE

Step 3) Then we choose the next smallest unmarked number, which is


3, and repeat the last step by marking the multiples of 3.

Eliminated multiples of 3
Representation EXAMPLE

Step 4) We repeat step 3 in the same way until x= or 5

Eliminated multiples of 5
CIRCULAR LINKED LIST EXAMPLE

Step 5) The remaining non-marked numbers would be the prime numbers


from 2 to 25.
Remaining are primes
CIRCULAR LINKED LIST
Complexity

The time and space complexity of The Sieve of Eratosthenes


algorithm is as follows:

• time complexity: Θ(N log log N)


• Space complexity: Θ(N)
PROGRAM
CIRCULAR LINKED LIST
Find all the prime numbers

import java.util.*;
public class Main{ System.out.println("List of prime
public static void main(String args[]) numbers upto given number are : ");
{ for (int i = 2; i< bool.length; i++) {
Scanner sc = new Scanner(System.in); if(bool[i]==true) {
System.out.println("Enter a number"); System.out.println(i);
int num = sc.nextInt(); }
boolean[] bool = new boolean[num]; }
for (int i = 0; i< bool.length; i++) { }
bool[i] = true; }
}
for (int i = 2; i< Math.sqrt(num);i++){
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}}
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

[email protected] +91 7815 095 095 +91 9019 921 340

You might also like