Presentation is loading. Please wait.

Presentation is loading. Please wait.

MODIFIED SIEVE OF ERATOSTHENES

Similar presentations


Presentation on theme: "MODIFIED SIEVE OF ERATOSTHENES"— Presentation transcript:

1 MODIFIED SIEVE OF ERATOSTHENES
-By Abhijit Pandey Student, CSE Narula Institute of Technology

2 NEED OF SIEVES Sieves are needed to generate Prime Numbers
Algorithms are used to make the generation of prime numbers faster

3

4 Sieve of Eratosthenes Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true. for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n : A[j] := false Output: all i such that A[i] is true.

5 Why List all numbers if I know that Most of them are Composite ?

6 All Primes greater than 3 can be expressed as 6n±1.
PROBLEMS Listing all numbers from 2 to ‘n’ To get Prime up-to ‘n’ (n-1) numbers are listed Except 2 all even numbers are Composite Numbers of Primes less than ‘n’ is approximately = x/(log x -1) All Primes greater than 3 can be expressed as 6n±1.

7 ABSTRACT Sieve of Atkin is currently known the most efficient sieve to generate prime numbers. It simply lists all numbers up-to a given limit ‘n’ and then performs some tasks on them. It has also been proved that except 2 and 3 any prime number can be expressed in the form of 6n±1. The basic idea is to merge these two concepts to make the algorithms a little faster. Instead of listing all the numbers we are going to list 2, 3 and only those numbers which are of the form of 6n±1. Then we simply multiply the numbers starting with 5 and then shifting to next prime from the list with others and also with itself, the product also exists in this list only. We mark these results as non-prime or composite but still they will be multiplied with others but they will be skipped as primary multiplication numbers to avoid repeating the products. Finally the list will be there with some marked as prime and some as composite and thus we get all the primes. Moreover we don’t list the numbers 6n-1 if dividing n by 10 gives 6 or 1as remainder and 6n+1 if dividing n by 10 gives 4 as remainder.

8 Modifications Eratosthenes New List all numbers from 2 to n
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n : A[j] := false List 2,3 and all numbers of the form 6n±1 Making two sets and marking the products up-to a limit as Composite. (Explained Later)

9 ALGORITHM List all numbers of the form 6n±1, if last digit of n is 6 or 1 then don’t list 6n-1 and if the last digit of n is 4 then don’t list 6n+1. Initially mark them all as prime. Starting from 7 multiply every prime with every number in the list (except 5) till the product exceeds the given number up-to which the primes should be found. Mark the products as composite. Move to next number marked as prime and multiply it with every number marked as prime as well as composite till the product exceeds the given number. Continue doing so until multiplying a prime with 7 gives number greater than the given number

10 PSEUDO-CODE Make a structure with flag and content. Make an array, a[], of the structure. Initially flag= prime INPUT ‘n’ List 2 and 3. i <- 2, j <- 1 while(a[i]<n) if(n%10=6 or 1) then a[i]=6n+1 ; i++ else if(n%10=4) then a[i]=6n-1 ; i++ Else a[i]= 6n-1; a[i+1]=6n+1; i=i+2 n++; repeat

11 PSEUDO-CODE (Contd.) For i =3 to k [where a[k]= √n]
if (a[i]*7>n) then break; Else For j=3 to k if(a[i]*a[j] >n) then break; Else mark the product as Composite. j++; repeat; k++;

12 BASIC OPERATION PRIME COMPOSITE 5 7 11 13 17 19 23 25 29

13 TIME COMPARISION INPUT Time for non-modified Sieve
Time for Modified Sieve 100 500 1000 5000 10000

14 NON-MODIFIED MODIFIED

15 LOOP RUN COMPARISION INPUT ERATOSTHENES MODIFIED 100 102 12 500 654 1000 1409 228 5000 8085 1460 10000 16979 3186

16 THANK YOU


Download ppt "MODIFIED SIEVE OF ERATOSTHENES"

Similar presentations


Ads by Google