Question
Eratosthenes used a technique that is known as the Sieve of Eratosthenes. Implement a program that computes all the primes up to some integer n,
Eratosthenes used a technique that is known as the Sieve of Eratosthenes.
Implement a program that computes all the primes up to some integer n, have this n be an integer you prompt the user to enter.
The algorithm pseudocode is the following: 1. Create a queue named queueOfIntegers, enqueue it with the consecutive integers 2 through n. 2. Create an empty queue to store primes, perhaps named queueOfPrimes. 3. Do 4. Get the next prime number, p, by removing the first value in queueOfNumbers. 5. Enqueue the value of p into queue of primes. 6. Create a new queue and fill this queue by doing the following: 7. While queueOfNumbers is not empty, dequeue the front number, if it is not divisible by p then enqueue this number to newQueue (queueOfPrimes.back() is our current prime number) 8. Assign the queueOfNumbers object with the newly created queue object 9. While (queueOfPrimes.back() < sqrt(n)) 10. Display the primes, which are all the values in the queueOfPrimes object.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started