Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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...
- //Works 12:43 PM 1/06/2019 as copied from onlinegdb.com But now need to expand last digit list up to a 10K file.
- //First try a small file viz lastdigit100.txt that contains about 100 last prime digits. Going to create a function out of file: C:\Users\Dell\Documents\Primes\ReadFileIntoArray0
- //Works OK but only 16 digits read in. Sat Jun 1 13:44:05 New Zealand Standard Time 2019
- // Got this version to work. Primedist4.c with 100-digit text file of consec prime last digits. Outputs distance files for 1,3,7,9 in files like distFile7.txt for distances for 7-1, 7-3, 7-7, 7-9.
- // Next try this program with larger files of last digits. Probably up to 10k long. Also use gawk etc to analise the data.
- #include <stdio.h>
- #include <stdlib.h>
- //int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };
- int ar[12000] ;
- int sz,numOfLastDigits;
- int distances[10];
- void showDistances(void);
- void match1(int anchor);
- void houseKeeping(void);
- void windUp(void);
- void writeDistances(int anch);
- void readLastDigitsFileIntoArray(void);
- int size;
- int newDistCounter = 0;
- FILE *fptr1,*f1,*f3,*f7,*f9, *myFile;
- int main () {
- houseKeeping();
- readLastDigitsFileIntoArray();
- // int ar[numOfLastDigits];
- // size = sizeof (ar) / sizeof (ar[0]);
- // printf("\n sizeof(ar) is %d and sizeof(ar[0]) is %d \n ",sizeof(ar),sizeof(ar[0]));
- size = numOfLastDigits;
- printf("\n size = %d \n",size);
- for (int i =0;i<size-3;i++) {
- match1(i);
- showDistances();
- printf("-%d -",i);
- }
- 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);
- }
- void readLastDigitsFileIntoArray(void) {
- // FILE *myFile;
- myFile = fopen("randoms10k1379A.txt", "r");
- printf("Starting to insert txt file into array called numberArray[] \n");
- fseek(myFile, 0L, SEEK_END);
- sz = ftell(myFile);
- numOfLastDigits = sz/3;
- rewind(myFile);
- printf("The size of file is: %d bytes\n ",sz);
- printf("So assuning number of last digits is: %d \n ",numOfLastDigits);
- //read file into array
- int big =16;
- int numberArray[sz];
- int i;
- if (myFile == NULL){
- printf("Error Reading File\n");
- exit (0);
- }
- for (i = 0; i < numOfLastDigits; i++){
- //for (i = 0; i < 16; i++){
- fscanf(myFile, "%d,", &numberArray[i] );
- printf(" %d %d \n", i,numberArray[i]);
- }
- /* for (i = 0; i < 16; i++){
- printf("Number is: %d\n\n", numberArray[i]);
- } */
- printf("done, index is %d \n",i);
- for (i=0;i< numOfLastDigits;i++) {
- //for (i=0;i< 10;i++) {
- ar[i] = numberArray[i];
- //ar[i] = numberArray[i];
- }
- fclose(myFile);
- // return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment