Implement a Sieve of Eratosthenesto find prime numbers. A sieve is implemented using a tvector of bool values, initialized so that all elements are true. To find primes between 2 and N, use tvector...


Implement a Sieve of Eratosthenesto find prime numbers. A sieve is implemented using a tvector of bool values, initialized so that all elements are true. To find primes between 2 and N, use tvector indices 2 through N, so you’ll need an (N +1)-element tvector.


1. Find the first entry that is true (initially this entry has index 2, because 0 and 1 do not count in the search for primes). We’ll call the index of the true entry p, since this entry will be prime.


2. Set each entry whose index is a multiple of p to false.


3. Repeat until all tvector elements have been examined


The process is illustrated in Figure 8.6 for the numbers 2 through 18. Circled numbers are true. In the topmost view of the array the first true cell has index 2, so all the even numbers (multiples of 2) are changed to false. These are shown as shaded entries in the diagram. The next true value is 3, so all multiples of 3 are changed to false (although 6, 12, and 18 have already been changed). In the third row no more new entries will be set to false that are not already false, and the primes have been determined (although the steps are repeated until all tvector elements have been examined).

Nov 17, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here