Ghulam Mohammad P.

asked • 03/21/21

Data Structure and Algorithm

An integer N>1 is called prime number if its only positive divisor are 1 and n; otherwise, n is called a composite number. For example, the following are the positive numbers less than 20:

2, 3, 5,7,11,13,17,19

If N>1 is not prime i.e. if n is composite then n must have a divisor K≠1 such that K≤√n or, in other words, K2 ≤ n...

Suppose we want to find all the prime number less than given number m such as 30. This can be done by “Sieve Method” which consists of the following steps

First list the 30 numbers

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,22,23,24,25,26,27,28,29,30    

Cross out 1 and multiple of 2 from the list as follows:

2,3,5,7,9,11,13,17,19,21,23,25,27,29

Since 3 is the first number following 2 that has not been eliminated, cross out the multiple of 3 from the list as follows

2,3,5,7,11,13,17,19,23,25,29

Since 5 is the first number following 3 that has not been eliminated, cross out the multiple of 5 from the list as follows

2,3,5,7,11,13,17,19,23,29

Now 7 is the first number following 5 that has not been eliminated, but 72 > 30. This means the algorithm is finished and the numbers left in the list are primes less than 30

2,3,5,7,11,13,17,19,23,29

Translate the sieve Method into an algorithm to find all prime numbers less than given number n. Analyze the algorithm for time complexity.

1 Expert Answer

By:

Patrick B. answered • 03/21/21

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.