Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need an answer to this question in simple C++ language using include , loop and if statements The above visualization kecps going until all

I need an answer to this question in simple C++ language using include , loop and if statements
image text in transcribed
image text in transcribed
image text in transcribed
The above visualization kecps going until all the numbers in the range are selected. Once done, all the composite numbers will be marked with 0 , and only the prime numbers will remain marked with I. The following is a pseudo-code for the Sieve of Eratosthenes algorithm, it uses an integer array to keep track of the primality of each number. The index of the array represents the number, and the value of the array is cither I or 0 to represent whether the number in the index is prime or not: When you write the function sieve, it will accept the array and its size as input. The function will then prepare the array according to the algorithm described in the pseudo code above. Printing the primes from the prepared array: The second function accepts an array that was prepared by the sieve function and its size. And simply prints all the numbers marked as prime inside it. The numbers are separated by commas whn printed. Improving Sieve: Once you have-implemented the two functions and tested them, you will be ready to improve your algorithm and make it faster by reducing the number of times your loops iterate in the function sieve. Let us look at the following figure that shows the Sicve of Eratosthenes method done on numbers from 2 to 120. Improving the outer loop: Let us consider the composite number 120 , it can be formed by multiplying the two numbers in each of the following pair: (1,120),(2,60),(3,40),(4,30),(5,24),(6,20),(8,15),(10, 12). Notice that 120 is around 10.95, and how all the numbers in the pairs above are spread around its square root: 12345681012012152024304060120 For each of the factors that are less than or equal to 120, there is a factor greater than or equal to 120 that can be multiplied with it to result in 120 . This can help us improve our code because we can be sure that 120 will be marked as composite by one of the numbers that are less than or equal to its square root. Therefore, we do not need to loop until 120 . only until 120 Of course, in this example 120 will be marked composite when we mark the multiples of 2 , but this is not always the case. For example, if we had 121 numbers, then 121 will only be marked as composite when we mark the multiples of 11 (the square root of 121). So it is always enough to iterate until the square ront of the numher, hut not less than that. This will change our code to look like this: 9ieve (array, size): set all values in array to 1 (prime) set array[0] to 0 (composite) set array[1] to 0 for each index 1 in array from 2 to sqrt(aize): for each multiple of i: set array[this multiple of 1 ) to 0 Improving the inner loop: Let us now look at the prime number 5 , notice how the finst multiple of 5 that is not yet marked as composite is 25 . All of 10,15 , and 20 were marked composite by previous smaller primes. The same is true if we look at any other prime number, for example 7 , the first multiple of 7 that is not yet marked as composite is 49 . All of 14,21,28,35, and 42 are marked composite by previous smaller primes. This means that instead of looping through the multiples of the prime number starting with the next multiple (2rime) we can start with the square of the prime ( prime 2). This will change our code one more time to look like this: \begin{tabular}{l} sieve (array, size): \\ set all values in array to 1 (prime) \\ set array[0] to 0 (composite) \\ set array[1] to 0 \\ for each index i in array from 2 to sqrt (size): \\ for each multiple of i starting from 12 : \\ set array[this multiple of i] to 0 \\ \hline \end{tabular} Note that i2 inight be a huge number, so it is advised to use long long to store it instead of int. To test the Sieve method against the original method in Task 3, run the program and ask it to output the prime numbers up to I million (1,000,000). Notice how the Sieve method is much faster than the original method in Task 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define marketing.

Answered: 1 week ago

Question

What are the traditional marketing concepts? Explain.

Answered: 1 week ago

Question

Define Conventional Marketing.

Answered: 1 week ago

Question

Define Synchro Marketing.

Answered: 1 week ago

Question

3. What information do participants need?

Answered: 1 week ago