Question: Write a Primes program that finds prime numbers using the Sieve of Eratosthenes, an algorithm devised by a Greek mathematician of the same name who

Write a Primes program that finds prime numbers using the Sieve of Eratosthenes, an algorithm devised by a Greek mathematician of the same name who lived in the third century BC. The algorithm finds all prime numbers up to some maximum value n, as described by the following pseudocode:

create a queue of numbers to process. fill the queue with the integers 2 through n inclusive. create an empty result queue to store primes. repeat the following steps: obtain the next prime p by removing the first value from the queue of numbers. put p into the result queue

Several web sites have nice descriptions and animations of this algorithm in action; consider searching for “Sieve of Eratosthenes” in your web browser.

create a queue of numbers to process. fill the queue with the integers 2 through n inclusive. create an empty result queue to store primes. repeat the following steps: obtain the next prime p by removing the first value from the queue of numbers. put p into the result queue of primes. loop through the queue of numbers, eliminating all numbers that are divisible by p. while (p is less than the square root of n). all remaining values in the numbers queue are prime, so transfer them to the result primes queue.

Step by Step Solution

3.44 Rating (173 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class MySolution void MySolutionint n ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (2 attachments)

PDF file Icon

2079_60f58f34f28fc_856965.pdf

180 KBs PDF File

Word file Icon

2079_60f58f34f28fc_856965.docx

120 KBs Word File

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!