The Prime Tableau and the Rational Primitive Mask

C code

October 2013

The C code is provided as a simple program that prints some number of lines of the Tableau, using the column method. You can compile and run this program on a Unix system (like the Mac). An example of its use is shown below. The part for creating the Tableau could easily be extracted from this program. The Javascript code is provided as an independent "module" that can be used in other programs. The C code also allocates the Tableau data as a single 2d array, so uses multiplication to get element offsets from rows and columns, and wastes half of the memory. The js code allocates an array for each row, so avoids this multiplication and is more memory efficient. An example of using the row method in C is available on the test page. The C code for the current page is also available as a file for download.

// bootstrap_pm.c
// generate rational primitive mask from scratch by copying binary sequences
// as a bonus, easily detect and enumerate prime numbers graphically
// 10/3/13
// Copyright Sky Coyote 2013

// compile with: cc -O3 -o bootstrap_pm bootstrap_pm.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
if (argc < 2)
   {
   printf("Usage: bootstrap_pm nlines\n");
   return 1;
   }
int nlines = strtol(argv[1], 0, 10);
   
int nx = nlines, ny = nlines;
char *pm = (char *) malloc(nx * ny); // rational primitive mask
int i, j;
for (i = 0; i < nx * ny; i ++) pm[i] = -1;

unsigned char *isPrime = (unsigned char *) malloc(ny); // prime mask
isPrime[0] = 0; // 0 is not prime

// the "column" method: first column
pm[0] = 1;
pm[nx] = 1;
for (j = 2; j < ny; j ++)
   pm[nx * j] = 0;
   
// all other columns
for (i = 1; i < nx; i ++)
   {
   int k = 0; // index into ith row sequence
   for (j = i; j < ny; j ++)
      {
      // set value in column to value in row sequence
      pm[i + j * nx] = pm[k + i * nx];
      // increment index and possibly wrap
      k ++;
      if (k >= i) k = 0;
      }
   }
   
// detect primes
for (j = 1; j < ny; j ++)
   {
   int ok = 1;
   // pm must be all 1s between (not including) first column and diagonal
   for (i = 1; i < j; i ++)
      if (pm[i + j * nx] == 0)
         {
         ok = 0;
         break;
         }
   isPrime[j] = ok;
   }
   
// print
for (j = 0; j < ny; j ++)
   {
   if (isPrime[j]) printf("%5d - ", j);
   else printf("        ");
   for (i = 0; i <= j; i ++)
      printf("%1d", pm[i + j * nx]);
   printf("\n");
   }

return 0;
}

The following is a short Unix session showing compiling and running the code:

2: ~/Sites/Tableau/Code/demo/C > cc -O3 -o bootstrap_pm bootstrap_pm.c
3: ~/Sites/Tableau/Code/demo/C > bootstrap_pm 
Usage: bootstrap_pm nlines
4: ~/Sites/Tableau/Code/demo/C > bootstrap_pm 33
        1
    1 - 11
    2 - 010
    3 - 0110
        01010
    5 - 011110
        0100010
    7 - 01111110
        010101010
        0110110110
        01010001010
   11 - 011111111110
        0100010100010
   13 - 01111111111110
        010101000101010
        0110100110010110
        01010101010101010
   17 - 011111111111111110
        0100010100010100010
   19 - 01111111111111111110
        010100010101010001010
        0110110010110100110110
        01010101010001010101010
   23 - 011111111111111111111110
        0100010100010100010100010
        01111011110111101111011110
        010101010101000101010101010
        0110110110110110110110110110
        01010100010101010101000101010
   29 - 011111111111111111111111111110
        0100000100010100010100010000010
   31 - 01111111111111111111111111111110
        010101010101010101010101010101010


©Copyright Sky Coyote, 2013. -- Tableau home