Saturday, April 27, 2019

Prime distances 1

To do: Get averages, means from 1 to 1, 1 to 3 .... 9 to 9 by looking at entries in distFileN.txt.
How to read big list of last disgits of prime numbers into array ar[] for use below.
How to get modes, medians, standard deviations etc.
How to read distFileN.txt in Excel to get stants and graphs.


This project is stored in online gdb here . It creates four new text files, distFile1.txt, distFile3.txt, distFile7.txt, distFile9.txt  that record distances from eg 1 to 1, 1 to 3, 1 to 7 and 1 to 9. Suspect some distances will be unexpected if just dealing with random numbers.

//PrimeDistances7a Looks at distance from each 1,3,7,9 (last digits of primes in list) to each of the 4 possibilities.
// eg given int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 } and looking at first 1 we see that the four distances are
// 4,1,2,3 from 1,3,7,9 respectively. Want to work out averages for each pair 1 to 1, 1 to 3 ....9 to 9. Should be more interesting than
// just random 1,3,7,9s...

#include <stdio.h>
int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };

int distances[10];
void showDistances(void);
void match1(int anchor);
void houseKeeping(void);
void windUp(void);
int size;
int newDistCounter = 0;
FILE *fptr1,*f1,*f3,*f7,*f9;

int main () {

  houseKeeping();
  size = sizeof (ar) / sizeof (ar[0]);

  for (int i =0;i<size-3;i++) {
      match1(i);
      showDistances();
  }
  return 0;
  windUp();
}


void match1(int anchor) {
    for(int l =0;l<10;l++) distances[l]=0;
    int i = anchor;
    int j = 1;
    newDistCounter=0;
    while ((i + j < size) && (newDistCounter<4) ){
        if (distances[ar[i + j]] == 0) {
            distances[ar[i + j]] = j;
            newDistCounter++;
        }
        j++;
    }
    writeDistances(ar[anchor]);
}

void showDistances(void) {
  for (int k = 0; k < 10; k++) {
      printf ("%d ", distances[k]); //init the places where distances will be recorded
  }
  printf(".....\n");
}
 
void writeDistances(int anch) {
    switch (anch) {
        case 1:
            fprintf(f1,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
            break;
        case 3:
            fprintf(f3,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
            break;
        case 7:
            fprintf(f7,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
            break;
        case 9:
            fprintf(f9,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
            break;
        default:
            printf("Not a prime\n");
    }
}

void houseKeeping(void) {
    int status;
    status = remove("distFile1.txt");
    f1 = fopen("distFile1.txt","a");
    status = remove("distFile3.txt");
    f3 = fopen("distFile3.txt","a");
    status = remove("distFile7.txt");
    f7 = fopen("distFile7.txt","a");
    status = remove("distFile9.txt");
    f9 = fopen("distFile9.txt","a");
}
void windUp(void) {
    fclose(f1);
    fclose(f3);
    fclose(f7);
    fclose(f9);
}
....................................
Here's what distFile1.txt will look like:
4 1 2 3
1 2 4 6
0 1 3 5

ie, top line above 4 1 2 3:
distance from 1 to 1 is 4 steps  (look at int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };
distance from 1 to 3 is 1 step,  look at int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };
distance from 1 to 7 is 2 steps, look at int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };
distance from 1 to 9 is 3 steps, look at int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };


No comments:

Post a Comment