( The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1\. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain \(1 .\) All other array elements will eventually be set to zero. You'll ignore elements 0 and 1 in this exercise. b) Starting with array subscript 2 , every time an array element is found whose value is 1 loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the clement with value 1 . For array subscript \(2,\) all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4,6 \(8,10, \text { etc. }) ;\) for array subscript \(3,\) all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts \(6,9,12,15,\) etc.); and so on. When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 2 and \(999 .\) Ignore element 0 of the array.

Short Answer

Expert verified
Use an array of 1000 elements to implement the Sieve of Eratosthenes, mark non-prime numbers, and then print out indices from 2 to 999 that remain true, which represent prime numbers.

Step by step solution

01

Initialize the Array

Create an array 'sieve' with 1000 elements, all initialized to 1 (true). The first two elements (index 0 and index 1) will not be used, as the problem states that we should ignore them since neither 0 nor 1 are prime.
02

Implement the Sieve of Eratosthenes

Start with the first prime number, 2. For each number 'i' starting from 2 up to the square root of the array size (since a non-prime number must have a factor less than or equal to its square root), if the array element at 'i' is 1 (true), then iterate through the array starting from 'i*i' and mark each multiple of 'i' (2*i, 3*i, 4*i, ...) by setting the element at that index to 0 (false). This signifies that the number is not prime.
03

Output the Prime Numbers

Iterate through the array from index 2 to 999. For each index 'j', if the value in the array is still 1 (true), then this index represents a prime number, as it was not marked as a multiple of any smaller number. Print this number.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Prime Numbers
A prime number is a fundamental concept in mathematics, particularly in number theory. These are numbers greater than 1 that have no divisors other than 1 and themselves. In other words, a prime number cannot be formed by multiplying two smaller natural numbers. For example, the first six prime numbers are 2, 3, 5, 7, 11, and 13. Notice that the number 2 is special—it is the only even prime number.

Understanding prime numbers is crucial because they are considered the building blocks of the natural numbers. Any natural number can be expressed as a product of primes, which is known as its prime factorization. This property is essential in various fields of mathematics and helps in solving problems related to divisibility, cryptography, and number theory.

Furthermore, prime numbers are infinite—there is no largest prime—and they become less frequent as numbers get larger. However, methods like the Sieve of Eratosthenes help us in systematically finding primes within a given range, which is an efficient way to handle tasks involving prime determination.
Array Initialization
Array initialization refers to the process of setting up an array with predefined values. In computer programming, an array is a collection of elements that can be accessed by an index, and initializing an array involves assigning values to its elements at the time of its creation.

In the context of the Sieve of Eratosthenes, initializing the array is a critical first step. Typically, all elements are initialized to a value of 1, representing 'true,' indicating that all numbers are initially considered prime. In the algorithm, this setup allows for a systematic approach to eliminating non-prime numbers. It's important to note that although the array indexes begin at 0, for this particular application, we disregard array elements at indexes 0 and 1 since they do not represent prime numbers.

Proper array initialization sets the stage for the algorithm to work correctly, ensuring that each number starts with the presumption of primality and only upon further inspection may be deemed otherwise.
Algorithm for Finding Primes
The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a specified integer. It is considered one of the most efficient ways to find small primes. The algorithm works by iteratively marking the multiples of each prime number starting from 2—the first prime number.

Here’s a simplified description of the algorithm implemented in three main steps:
  • Initialize an array (let's call it 'sieve') with elements set to 1 (true). Ignore the elements at index 0 and 1.
  • Starting from the first prime number, 2, check each element to see if it’s marked as 1 (true). If so, loop through further elements and set every multiple of this index to 0 (false), indicating that those numbers are not prime.
  • Once all multiples have been marked, the elements still set to 1 indicate prime numbers. These indices are the prime numbers within the given range.

The beauty of this algorithm lies in its simplicity and efficiency. After initialization, it takes advantage of the fact that the multiples of each number do not need to be checked beyond that number's square root. This is because if a number is divisible by a factor larger than its square root, it must also be divisible by a factor smaller than the square root, and thus it would have already been marked as a non-prime. Moreover, as the process iterates, fewer non-prime numbers remain, making the sieving process increasingly faster as it progresses.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

(Double Array Questions) Answer the following questions regarding an array called table: a) Declare the array to be an integer array and to have 3 rows and 3 columns. Assume that the constant variable arraysize has been defined to be 3 b) How many elements does the array contain? c) Use a for statement to initialize each element of the array to the sum of its subscripts. Assume that the integer variables \(i\) and \(j\) are declared as control variables. d) Write a program segment to print the values of each element of array table in tabular format with 3 rows and 3 columns. Assume that the array was initialized with the declaration int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } }; and the integer variables i and j are declared as control variables. Show the output.

(Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you'll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important). Pick five causes that are important to you (e.g., political issues, global environmental issues). Use a one-dimensional array topics (of type string) to store the five causes. To summarize the survey responses, use a 5 -row, 10 -column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including: a) \(A\) tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic. b) To the right of each row, show the average of the ratings for that issue. c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

(Fill in the Blanks) Fill in the blanks in each of the following: a) The names of the four elements of array p (int p[4];) are ___, ___ , ___ and ___. b) Naming an array, stating its type and specifying the number of elements in the array is called __ the array. c) By convention, the first subscript in a two-dimensional array identifies an element's ___ and the second subscript identifies an element's ___. d) An m-by-n array contains __ rows, __ columns and ___ elements. c) The name of the element in row 3 and column 5 of array \(d\) is __.

(Single Array Questions) Write single statements that perform the following one-dimensional array operations: a) Initialize the 10 elements of integer array counts to zero. b) Add 1 to each of the 15 elements of integer array bonus. c) Read 12 values for double array month 7 yTemperatures from the keyboard. d) Print the 5 values of integer array bestscores in column format.

(Print a String Backuard) Write a recursive function stringReverse that takes a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free