Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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. 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions