Maths and Computing
Home
Books
Mathematics
Binary
Computing
Coding
Information
Maths and Computing JavaScript Examples
This tool will run small JavaScript programs. It is essentially intended for use with the book "An Introduction to Mathematics for Computing and IT Practitioners". Click an entry on the right to select one of the book's code examples.
1.3.2: Working out the triangular numbers
2.3.1: Storing a vector in an array
2.3.2: Storing a matrix in an array
3.2.3: Multiplying two 2 by 2 matrices
5.3.2: Solving a simultaneous equation
6.4.2: Finding the minimum spanning tree
7.5.1: Calculating the nth term of a given sequence V1
7.5.2: Calculating the nth term of a given sequence V2
8.2.2: Quick Sort
9.6.3: Simulation Scenario 1
9.6.4: Simulation Scenario 2
11.1.5: Converting a decimal number to binary
11.5.3: Converting binary numbers to Gray code
13.4.3: Converting an 8-bit character to MIME Base 64
15.1.2: Calculating a sequence of prime numbers
15.2.3: Euclids Algorithm
15.3.2: Calculating the square root of a binary number
15.5.2: Working out the day of the week
<html>
<body>
<script type ="text/javascript"> <title>
Finding the minimum spanning tree
</title>
</head>
<body>
<script type ="text/javascript">
// Define the place names in the array v var v = new Array("E","Br","Bi","S","L","R","N","C","F"); // Define the array M to indicate which vertices have been marked var m = new Array(1,0,0,0,0,0,0,0,0); // Define the 2-dimensional array d and initialise // with the weight matrix values var w = new Array(9); w[0] = new Array(0,70,0,0,0,0,0,0,0); w[1] = new Array(70,0,90,0,120,0,0,0,0); w[2] = new Array(0,90,0,0,115,35,0,0,0); w[3] = new Array(0,0,0,0,80,0,0,0,0); w[4] = new Array(0,120,115,80,0,80,0,60,70); w[5] = new Array(0,0,35,0,80,0,40,0,0); w[6] = new Array(0,0,0,0,0,40,0,0,0); w[7] = new Array(0,0,0,0,60,0,0,0,0); w[8] = new Array(0,0,0,0,70,0,0,0,0); var d = 0; // Total distance var s; // Shortest distance var p,q; // Marked and unmarked vertices var i,j,k; // General variables // Select a vertex and mark it m[0] = 1; // Find the eight edges in the minimum spanning tree // representing an iteration in the algorithm for(k=0;k<8;k++) { // Set an initial minimum edge length, s, that // is larger than all of the edge weights s = 121; // For each marked vertex for(i=0; i<9;i++) { if(m[i]==0) continue; // For each unmarked vertex for(j=0;j<9;j++) { if(m[j]==1) continue; // If the weight of the edge is less than s if(w[i][j]>0 && w[i][j]
"); } // Output the total path length document.writeln("Total Distance = " + d + "
");
</script>
</body>
</html>