SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Standard Template Library
Exercise:
Programming Projects
Question:3 | ISBN:9780132846813 | Edition: 5

Question

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 x = y*z. The Greek mathematician Erathosthenes (pronounced Er-ah–tos-thin- eze) gave an algorithm for finding all prime numbers less than some integer N. This algorithm is called the S ieve of Erathosthenes . It works like this: Begin with a list of integers 2 through N . The number 2 is the first prime. (It is instructive to 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, which is 3, is the next prime. The multiples of 3 are not primes . Cross these 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 this fashion until we reach N . All the numbers not crossed off the list are primes.

a. Write a program using this algorithm to find all primes less than a user- upplied 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.

Improvements:

c. Actually, we do not need to go all the way to N. You can stop at N/2. Try 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 the 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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.