<!--
Module 5 - assignment 1 - sheldon pasciak
Description of processes involved: essentially, starts with a list of numbers, realizing the first is prime (2). Incrementally stores all non factors of this first element (2) in one stack. Adds that first element (2) to primes stack , the loops with a new first number and continues the process until the numbers list [L1] IS EMPTY.
Module 5 - Queues
Assignment 1 -
You are going to create a List. You will then fill it with numbers consecutively numbered from 2 to n where n is entered by the user.
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 - Push this into Q1 and remove it from L1
3. Iterate through each element of L1 and if the value is divisible by 2 remove 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
-->
<input type="number" id="maximumValue" value=10 />
<input type="button" id="runButton" value="Click to Run" />
<br />
<p id="output"></p>
JavaScript
// module 5 - assignment 1 - sheldon pasciak
// observations ... using a stack structure to do the storage
// rather than a mix of structures is a bad choice
// I find that that poping elements to access them destroys my data
// needing that it be saved away and built in again, to include
// reversing the order through successive empties from one stack
// then into another
// I will reengineer this is a version 2 .. HOPEFULLY I find the time.
// this implementation uses stacks and a very inefficient method of
// moving data between stacks to facilitate reordering
// observations -- this stack uses node pointers and has no back storage array
// new stack .. unlimited size uses new node pointers
function Stack() {
this.top = null;
this.size = 0;
}
//nodes to store data -- this.previous is pointer to previous node
function Node(data) {
this.data = data;
this.previous = null;
}
//add new node, make this new node the head
Stack.prototype.push = function(value) {
var node = new Node(value);
node.previous = this.top;
this.top = node;
this.size++;
return this.top;
}
//a pop effectively just makes a new top being the previous - the old top is lost data and memory waste
Stack.prototype.pop = function() {
if (this.size>0) {
temp = this.top;
this.top = this.top.previous;
this.size--;
return temp.data;
} else {
return null;
}
}
//a size>=1 indicates there are nodes remaining ... size variable adjusted in pop/push
Stack.prototype.isEmpty = function() {
return (this.size<=0);
}
//facilitates a view as [2 3 4 5 6... etc] based on element data
Stack.prototype.printString = function() {
var current=this.top;
var outp="[";
while (current) {
outp += current.data + " ";
current = current.previous; // becomes null at top
}
outp+="]";
return outp;
}
//console display of data from top down without using...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.