Thursday, January 31, 2019

Prime dots

I wanted to show that the last digits in prime numbers did not occur randomly, rather the 1,3,7,9 that they all end with occur in patterns. So I found some random number lists and played with them using gawk and c until I had a nice list of the last digits of all the primes up to one million. There are about 79,000 of them. This is the file that's opened in the following line:
ptr_file =fopen("upTo1GlastNum3.txt","r");

I gave each of 1,3,7,9 a dot-colour and put them in a 79k 256-colour BMP using great library I found called EasyBMP.

To compare the dot picture I did the same for a 640 x 480 BMP of random 1,3,7,9 digits generated bu normal random number function in C.

Here is the the picture that's generated from actual prime numbers.:


And here's the one generated by rand() in C.


There's more in the lower picture because I kept going til full 640 x 480 BMP was filled. That is 307K dots whereas the number of dots generated from real primes came from a list of only 78k digits = dots.
Upshot. They look very similar. Not sure this BMP method is a good way to generate non-randomness of prime last digits.

The program I used follows.

//BMPDots7 makes a BMP of last digit of all randoms up to 1M. Plus does similar to program generated random numbers' last digit.
// Gives two BMPs. One for last digit of prmes, other for random 1,3,7,9. Hard to tell the difference.
//To do: Create file of 1,3,7,9 from rand(). Read file in a place relevant dot on BMP.

#include "EasyBMP.h"
#include<stdlib.h>
#include<conio.h>
using namespace std;
int main( int argc, char* argv[] )
{
    
BMP PrimePic0;
PrimePic0.SetSize(640,480);
PrimePic0.SetBitDepth(8);

int xxcount3 = 0; 
int ones2=0;
int threes2=0; int sevens2=0; int nines2=0; long nonPrimes2 = 0; long allLines=0;
FILE *ptr_file; 
char buf[1000];
ptr_file =fopen("upTo1GlastNum3.txt","r");
int h=0; int k=0;
while ((fgets(buf,1000, ptr_file)!=NULL) && (k<480)){
allLines++;
switch (buf[0]) {

case '1':
PrimePic0(h,k)->Red = 255; 
PrimePic0(h,k)->Green = 255;
PrimePic0(h,k)->Blue = 0;
ones2++;
break;
case '3':
PrimePic0(h,k)->Red = 255; 
PrimePic0(h,k)->Green = 0;
PrimePic0(h,k)->Blue = 0;
threes2++;
break;
case '7':
PrimePic0(h,k)->Red = 0; 
PrimePic0(h,k)->Green = 255;
PrimePic0(h,k)->Blue = 0;
sevens2++;
break;
case '9':
PrimePic0(h,k)->Red = 0; 
PrimePic0(h,k)->Green = 0;
PrimePic0(h,k)->Blue = 255;
nines2++;
break;
default:
cout << "Not prime" << allLines <<endl;
nonPrimes2++;
printf("Non prime");
}
h++;
if (h==640) {
h=0;
k++;
}
}
fclose(ptr_file);
PrimePic0.WriteToFile("prime0Pic.bmp");
//Now do case where 1,3,7,9 are generated by random numbers via c-rand().
BMP AnImage;
AnImage.SetSize(640,480);
AnImage.SetBitDepth(8);
cout << "File info:" << endl; 
cout << AnImage.TellWidth() << " x " << AnImage.TellHeight() << " at " << AnImage.TellBitDepth() << " bpp" << endl;
cout << "colors: " << AnImage.TellNumberOfColors() << endl;
cout << "(" << (int) AnImage(14,18)->Red << "," << (int) AnImage(14,18)->Green << "," << (int) AnImage(14,18)->Blue << "," << (int) AnImage(14,18)->Alpha << ")" << endl; 


int randDigit,i,j,ones,threes,sevens,nines;
long nonPrimes=0;
long allNums = 0;
j=0; ones=threes=sevens=nines=0; i=0;

while (j<480) 
{
randDigit = rand()%10;
allNums++;
switch (randDigit)
{
case 1:
AnImage(i,j)->Red = 255; 
AnImage(i,j)->Green = 255;
AnImage(i,j)->Blue = 0;
ones++;
break;
case 3:
AnImage(i,j)->Red = 255; 
AnImage(i,j)->Green = 0;
AnImage(i,j)->Blue = 0;
threes++;
break;
case 7:
AnImage(i,j)->Red = 0; 
AnImage(i,j)->Green = 255;
AnImage(i,j)->Blue = 0;
sevens++;
break;
case 9:
AnImage(i,j)->Red = 0; 
AnImage(i,j)->Green = 0;
AnImage(i,j)->Blue = 255;
nines++;
break; 
default:
nonPrimes++;
i--;
}
i++;
if (i==640) {
i=0;
j++;
}
}
AnImage.WriteToFile("copied1dots3.bmp");
cout << "Non primes= " << nonPrimes << endl;
cout << "Ones =" << ones;
cout << "  Threes =" << threes;
cout << "  Sevens =" << sevens;
cout << "  Nines =" << nines << endl;
cout << "  xxcount3 " << xxcount3 << endl;
cout << "  allNums " << allNums << endl;
cout << "  ones2 " << ones2 << endl;
cout << "  Threes2 =" << threes2;
cout << "  Sevens2 =" << sevens2;
cout << "  Nines2 =" << nines2 << endl;
cout << "Non primes2 = " << nonPrimes2 << endl;
cout << "allLines = " << allLines << endl;
cout << "Sum of primes = " << ones2+threes2+sevens2+nines2 << endl;
 return 0;
}





No comments:

Post a Comment