Using jsfiddle. Please separate HTML and JAVASCRIPT. You are going to create a Q
ID: 3685004 • Letter: U
Question
Using jsfiddle. Please separate HTML and JAVASCRIPT. You are going to create a Queue (specific type of Linked List). You will then fill it with numbers consecutively numbered from 2 to n where n is entered by the user. When creating your Queue object use the correct function names for enqueue and dequeue. Create a second List - this one should be a Queue - it is empty. Once you have the first List filled we are going to use a technique called Sieve of Eratosthenes which uses first queue to fill the second queue. You will need to look at the algorithm for this https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes Here is the simple method to do this - L1 is the first list, Q1 is the Queue. 1. Go to 1st element in L1 (which will be 2). 2. Because it exists - Enqueue this into Q1 and dequeue it from L1 3. Iterate through each element of L1 and if the value is divisible by 2 dequeue it. 4. When done go back to the beginning of the list (the value will be 3 the second time around) 5. Print Iteration 1, Value of L1 (all elements) and Q1 (all elements) 6. Repeat steps 2-5 with this new element - repeat until L1 is empty. Sample output with input 10 Iteration 0: L1 = 2 3 4 5 6 7 8 9 10, Q1 = , Iteration 1: L1 = 3 5 7 9, Q1 = 2 Iteration 2: L1 = 5 7, Q1 = 2 3 Iteration 3: L1 = 7, Q1 = 2 3 5 Iteration 4: L1 = , Q1 = 2 3 5 7
Explanation / Answer
<title>Implement Sieve of Eratosthenes</title>
<body>
<section>
<h1>Queue Implementation</h1>
<label for="element">Element Limit:</label>
<input type="text" id="element"><br>
<label> </label>
<input type="button" id="add" value="Add to Queue" >
<input type="button" id="display" value="Display Results" ><br>
<h2>Results</h2>
<textarea id="results"> </textarea>
</section>
</body>
jscript code:
function Queue() {
this.dataStore = [];
this.enqueue = enqueue;
this.dequeue = dequeue;
this.front = front;
this.back = back;
this.toString = toString;
this.empty = empty;
}
//The enqueue() function adds an element to the end of a queue:
function enqueue(element) {
this.dataStore.push(element);
}
//The dequeue() function removes an element from the front of a queue:
function dequeue() {
return this.dataStore.shift();
}
//examine the front and back elements of a queue using these functions:
function front() {
return this.dataStore[0];
}
function back() {
return this.dataStore[this.dataStore.length-1];
}
//toString() function to display all the elements in a queue:
function toString() {
var retStr = "";
for (var i = 0; i < this.dataStore.length; ++i) {
retStr += this.dataStore[i] + " ";
}
return retStr;
}
//if a queue is empty:
function empty() {
if (this.dataStore.length == 0) {
return true;
} else {
return false;
}
var L1 = new Queue();
var Q1=new Queue();
var l1=L1.getLength();
var l2=Q1.getLength();
function Queue() {
this.dataStore = [];
this.enqueue = enqueue;
this.dequeue = dequeue;
this.front = front;
this.back = back;
this.toString = toString;
this.empty = empty;
}
//The enqueue() function adds an element to the end of a queue:
function enqueue(element) {
this.dataStore.push(element);
}
//The dequeue() function removes an element from the front of a queue:
function dequeue() {
return this.dataStore.shift();
}
//examine the front and back elements of a queue using these functions:
function front() {
return this.dataStore[0];
}
function back() {
return this.dataStore[this.dataStore.length-1];
}
//toString() function to display all the elements in a queue:
function toString() {
var retStr = "";
for (var i = 0; i < this.dataStore.length; ++i) {
retStr += this.dataStore[i] + " ";
}
return retStr;
}
//if a queue is empty:
function empty() {
if (this.dataStore.length == 0) {
return true;
} else {
return false;
}
}
//Return HTML Element object
var $ = function (id) {
return document.getElementById(id);
}
function Sieve_Of_Eratosthenes(limit) {
var theOutput = "";
var iter = 0;
var primeFactor = 2;
var mathInput = 0; // holds dequeue'd value as lists are used
var L1 = new Queue(); // numbers entered by user
var Q1 = new Queue(); // list of primes after sieve method
var saveList = new Queue(); // non divisors of current Prime for use in next iteration
//add numbers to L1 from 2 up to userNumber
for (var i=2;i<=limit;i++){
L1.enqueue(i);
}
theOutput += "Iteration: " + iter + " L1: " + L1.toString() + ", Q1: " + Q1.toString() + "<br />";
do {
// get first number from L1
// add it to primes List
// use it as divisor, check remaining elements in L1
primeElem = 2;
Q1.enqueue(primeElem); // at this point the first element of L1 is 2
while (!(L1.empty())) {
mathInput = L1.dequeue().data;
if ( mathInput % primeElem === 0 ) {
// if it divides, lose it, it's a factor of current prime
} else {
saveList.enqueue(mathInput);
}
}
//L1 is now empty, but we stored values that weren't factors of previous prime
//we need to check this in the next iteration
//empty saved list into L1 and continue until there is nothing more to add to L1 to filter
while (!(saveList.empty())) {
var saved = saveList.dequeue().data;
L1.enqueue(saved);
}
iter++;
theOutput += "Iteration: " + iter + " L1: " + L1.toString() + ", Q1: " + Q1.toString() + "<br />";
} while (!(L1.empty()))
document.getElementById("results").innerHTML = theOutput;
}
//click button display results using
//the sieve of Eratosthenes algorithm
$("#display").click(function(){
Sieve_Of_Eratosthenes( parseInt($('#element').val()) );
});
/*
var Sieve_Of_Eratosthenes = function (n) {
this.queue = function () {
var q3 = new Queue();
var q4 = new Queue();
for (var i = 0; i < n; i++) {
q3.enqueue(i);
}
return q3;
};
this.sieve = function () {
var sieve = this.q3();
var limit = Math.sqrt(n);
for (var i = 2; i <= limit; i++) {
if (sieve[i]) {
for (var j = i * i; j < n; j += i) {
sieve[j] = false;
}
}
}
var = [];
for (var k = 2; k < sieve.length; k++) {
if (sieve[k]) {
q4.push(sieve[k]);
}
}
return arr;
};
};
$("element").keypress(function (e) {
if (e.which == 2) {
var input = $("#input").val();
var era = new Eratosthenes(input);
var sieve = era.sieve().join(" ");
$("#output").html(sieve);
}
});
window.onload = function () {
//Event Handler for Display Results button
$("add").onclick = add_element();
$("display").onclick =Eratosthenes(2);
} //end window.onload
*/
css code
article, aside, figure, footer, header, nav, section {
display: block;
}
body {
font-family: Arial, Helvetica, sans-serif;
background-color: white;
margin: 0 auto;
padding: 10px 20px;
width: 550px;
border: 3px solid blue;
}
h1 {
color: green;
margin-top: 0;
margin-bottom: .5em;
}
h2 {
color: purple;
font-size: 120%;
padding: 0;
margin-bottom: .5em;
}
section {
padding: 1em 2em;
}
label {
float: left;
width: 3em;
text-align: right;
}
input {
margin-left: 1em;
margin-bottom: .5em;
}
textarea {
width: 200px;
height: 150px;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.