Question: Complete Sieves of Eratosthenes source code that uses only two queues instead of three. You do not need to change any of the existing Java
Complete Sieves of Eratosthenes source code that uses only two queues instead of three. You do not need to change any of the existing Java code that is provided
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. Get the next prime number, p, by removing the first value in queueOfNumbers.
4. Enqueue the value of p into queue of primes.
//Sieves17.java import java.util.Scanner; public class Sieves17 { public static void main(String[] args) { boolean verboseOutput=false; if ( args.length == 1 ) verboseOutput = args[0].equalsIgnoreCase("--VERBOSE"); Scanner kb = new Scanner(System.in); System.out.print("Enter an integer value of N: "); int n = kb.nextInt(); // 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. int p; do { // 3. Get the next prime number, p, by removing the first value in queueOfNumbers. if ( verboseOutput ) System.out.println("Dequeuing a prime number: "+p); // 4. Enqueue the value of p into queue of primes. if ( verboseOutput ) { System.out.print("Content of: queueOfPrimes: "); queueOfPrimes.display(); System.out.println(); } int size=queueOfIntegers.size(); if ( size==1 ) { if ( queueOfIntegers.back() % p != 0 ) queueOfPrimes.enqueue(queueOfIntegers.dequeue()); size--; } for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
