The Prime Tableau and the Rational Primitive Mask

Javascript code

October 2013

The js code is very short and is provided as an independent function and prototype methods (like a class). Its use should be pretty obvious. To see this code used in an real program, show the page source, and then the js stream, of the online demo. The js code shows how to allocate several arrays for the Tableau, rather than one big array, so avoids the multiplication used in the C code to get element offsets from rows and columns. The code for this page is also available as a file for download.

```// PrimitiveMask.js
// Prime Tableau demo
// 10/03/13
// Copyright Sky Coyote 2013

// input:              number of lines desired
// properties:     pm: primitive mask octant data
//                pm2: prime number mask
//             primes: list of primes

function PrimitiveMask(_ny) { with (this)
{
//console.log("PrimitiveMask(" + _ny + ")");

this.ny = _ny;
this.nx = _ny;

generateTable();
findPrimes();
}}

PrimitiveMask.prototype.generateTable = function () { with (this)
{
// initialize
this.pm = new Array(ny);
for (var j = 0; j < ny; j ++)
{
pm[j] = new Array(j + 1);
for (var i = 0; i <= j; i ++) pm[j][i] = -1;
}

// create Tableau by the "column" method: initialize first column
pm = 1;
pm = 1;
for (var j = 2; j < ny; j ++)
pm[j] = 0;

// generate all other columns
for (var i = 1; i < nx; i ++)
{
var k = 0; // index into ith row sequence
for (var j = i; j < ny; j ++)
{
// set value in column to value in row sequence
pm[j][i] = pm[i][k];
// increment index and possibly wrap
k ++;
if (k >= i) k = 0;
}
}
}}

PrimitiveMask.prototype.findPrimes = function () { with (this)
{
// initialize
this.pm2 = new Array(ny);
for (var j = 0; j < ny; j ++) pm2[j] = 0;

// find contiguous sequences of 1's in Tableau
this.primes = [];
for (var j = 1; j < ny; j ++) // 0 is not prime
{
var isPrime = true;
for (var i = 1; i < j; i ++)
if (pm[j][i] == 0)
{
isPrime = false;
break;
}
if (isPrime)
{
pm2[j] = 1;
primes.push(j);
}
}
}}
```

The following code is a scanner for the Tableau when it is input as a long string either with or without line breaks. This code looks for increasing length blocks of 1's. To see this code used in an real program, show the page source, and then the js stream, of the scanner demo. The code for this page is also available as a file for download.

```// PTScanner.js
// Prime Tableau scanner
// 10/7/13
// Copyright Sky Coyote 2013

// scan string for increasing length blocks of 1's
// input:      Tableau string with or without line breaks
// properties: primes: list of primes

function PTScanner(s) { with (this)
{
this.string = s;
this.position = 0;
this.currentLength = 0;
this.primes = [];
}}

// find the next prime sequence in string
PTScanner.prototype.findNextPrime = function () { with (this)
{
// skip ahead until first 0 and output 1
if (position == 0)
{
while (string[position] != "0") position ++;
primes.push(1);
return 1;
}

// look for block of 1's longer than previous block
var length = 0;
var found = false;
while (!found)
{
if (position >= string.length) return 0;
var bit = string[position];
if (bit == "0")
{
// found it?
if (length > currentLength) break;
// otherwise clear count
length = 0;
}
else if (bit == "1")
{
length ++;
}
position ++;
}

// update and add prime
position ++;
currentLength = length;
primes.push(length + 1);
return 1;
}}

// reset to beginning of string
PTScanner.prototype.reset = function () { with (this)
{
position = 0;
currentLength = 0;
primes = [];
}}
```

©Copyright Sky Coyote, 2013. -- Tableau home