Problem

A prime number is an integer greater than 1 and divisible only by itself and 1. An integer...

A prime number is an integer greater than 1 and divisible only by itself and 1. An integer x is divisible by an integer y if there is another integer z such that x = y * z. The Greek mathematician Eratosthenes (pronounced: Er-ah- tos-thin-eeze) gave an algorithm, called the Sieve of Eratosthenes, for finding all prime numbers less than some integer N. The algorithm works like this: Begin with a list of integers 2 through N. The number 2 is the first prime. (It is instructive Lo consider why this is true.) The multiples of 2, that is, 4, 6, 8, etc., are not prime. We cross these off the list. Then the first number after 2 that was not crossed off is the next prime. This number is 3. The multiples of 3 are not primes. Cross the multiples of 3 off the list. Note that 6 is already gone, cross off 9, 12 is already gone, cross off 15, etc. The first number not crossed off is the next prime. The algorithm continues on in this fashion until we reach N. All die numbers not crossed off the list are primes.

a.Write a program using this algorithm to find all primes less than a user-supplied number N. Use a vector container for the integers. Use an array of bool initially set to all true to keep track of crossed-off integers. Change the entry to false for integers that are crossed off the list.

b.Test for N = 10, 30, 100, and 300.

We can improve our solution in several ways:

c.The program does not need to go all the way to N. It can stop at N/2, ‘fry this and test your program. N/2 works and is better but is not the smallest number we could use. Argue that to get all the primes between 1 and N die minimum limit is the square root of N.

d.Modify your code from part (a) to use the square root of N as an upper limit.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 18
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT