Question

4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a...

4.3 Ch 4 Ex 14 - sieve of Erotosthenes

In this lab, you will write a program to compute all the prime numbers between 2 and some other integer, using an algorithm called the Sieve of Erotosthenes.

First you will read, from standard input, the integer that will be the upper limit of the range of primes. Call it max.

Then, create a vector<int> with max elements, and call it is_composite. The result of the Sieve will be that is_composite[i] will be 1 if i is composite, and 0 if it is prime.

We'll ignore entries 0 and 1; we really don't care about them. Here is pseudocode for the Sieve algorithm:

    for every integer i from 2 to max
        if i is not composite
            for every multiple of i up to max
                mark that multiple as composite

The numbers for which is_composite[i] remains 0 are prime numbers.

For example, say max is 30. We start out with is_composite looking like this:

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

The first time through outer the loop, we see that 2 is prime, so we set all its multiples (4, 6, 8, …, 28) to 1 to mark them as composite.is_composite now looks like this:

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

The next time through the outer loop, we see that 3 is also prime, so we mark its multiples (6, 9, 12, …, 27) as composite:

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

Next, we see that 4 is composite, so we don't need to mark its multiples. This makes sense, since all multiples of 4 are also multiples of 2, and we've already marked those as composite.

We then see that 5 is prime, and so we mark all its multiples (10, 15, 20, 25) as composite:

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

When we're done, we'll see that the elements of is_composite that are still 0 - 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29 - are all prime.

Write out the primes on a single line, with a space after each one. End the output with a newline. For example, if the input is 30, the output should be:

2 3 5 7 11 13 17 19 23 29 

Here are some hints concerning a couple of optional optimizations you can make:

  1. The outer loop does not have to run all the way to max. The next hint may help you figure out what the limit of the outer loop could be.
  2. When marking multiples, you don't have to start at i * 2; you can actually start at a higher value. Hint: You may find the sqrt()function useful; if so, you need to include the <cmath> header.

Notes:

All the elements of a vector<int> are automatically initialized to 0.

It may seem that using a vector<bool> would make more sense. Unfortunately, due to a well-intentioned but ultimately faulty attempt at treating vector<bool> as an optimized special case, its use is not recommended.

LAB

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

int main() {
int max;
cin>>max;
vector<int> is_composite(max + 1);

for(int i=2;i<=sqrt(max);i++){
if(!is_composite[i]){
for(int j=i*2;j<=max;j = j += i){
is_composite[j] = 1;
}
}
}

for(int i=2;i<=max;i++){
if(!is_composite[i])
cout<<i<<" ";
}
cout<<endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a...

    4.3 Ch 4 Ex 14 - sieve of Erotosthenes In this lab, you will write a program to compute all the prime numbers between 2 and some other integer, using an algorithm called the Sieve of Erotosthenes. First you will read, from standard input, the integer that will be the upper limit of the range of primes. Call it max. Then, create a vector<int> with max elements, and call it is_composite. The result of the Sieve will be that is_composite[i]...

  • PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than...

    PYTHON! The Sieve of Eratosthenes THANKS FOR HELP! A prime integer is any integer greater than 1 that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: Create a list with all elements initialized to 1 (true). List elements with prime indexes will remain 1. All other elements will eventually be set to zero. Starting with list element 2, every time a list element is found...

  • After discussing the issue with you and others in the Finance department, the manager decided to...

    After discussing the issue with you and others in the Finance department, the manager decided to change the payment terms for Accounts Receivable. The payment time remained at 14 days, but the penalty for late payments was increased. After about 3 months under the new payment terms, you asked the manager to collect a second stratified random sample. The data is presented in the second column of the worksheet (see the tab titled, “Data – Case #2) under the Heading...

  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

  • In this lab you will convert lab5.py to use object oriented programming techniques. The createList and...

    In this lab you will convert lab5.py to use object oriented programming techniques. The createList and checkList functions in lab5.py become methods of the MagicList class, and the main function of lab6.py calls methods of the MagicList class to let the user play the guessing game.                              A. (4pts) Use IDLE to create a lab6.py. Change the 2 comment lines at the top of lab6.py file: First line: your full name Second line: a short description of what the program...

  • A soft drink manufacturer uses fire agents to handle premium distribution for is various products. The marketing director desired to study the timeliness with which the premiums are distributed. Twent...

    A soft drink manufacturer uses fire agents to handle premium distribution for is various products. The marketing director desired to study the timeliness with which the premiums are distributed. Twenty transactions for each agent were selected at random and the time lapse (in days) for handling each transaction was determined. The results follow: Agent 1 Agent 2 Agent 3 Agent 4 Agent 5 24 18 10 15 33 24 20 11 13 22 29 20 8 18 28 20 24...

  • Task 2 Write a shell script program call cal3.sh to do the following. 1. Print the...

    Task 2 Write a shell script program call cal3.sh to do the following. 1. Print the output "you must provide at least one month" when there is no argument 2. Print the calendar with specified month matching the argument when there is one argument $ ./cal3.sh 1 January 2016 Su Mo Tu We Th Fr Sa 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27...

  • In C++ Make a program that takes a positive integer 'n' and write an n x...

    In C++ Make a program that takes a positive integer 'n' and write an n x n matrix whose entries are listed as snail Example: (See picture) for n=6 Ej. Para n-6 2 2 2 20 21 22 23 24 7 19 32 33 34 258 18 31 36 35 26 9 17 30 29 28 27 10 16 15 14 13 12 11

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

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
ADVERTISEMENT
ADVERTISEMENT