prjbrook

primDist4E.c

Jun 12th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. //PrimeDistances7a Looks at distance from each 1,3,7,9 (last digits of primes in list) to each of the 4 possibilities.
  2. // 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
  3. // 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
  4. // just random 1,3,7,9s...
  5. //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.
  6. //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
  7. //Works OK but only 16 digits read in. Sat Jun 1 13:44:05 New Zealand Standard Time 2019
  8. // 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.
  9. // Next try this program with larger files of last digits. Probably up to 10k long. Also use gawk etc to analise the data.
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. //int ar[] = { 1, 3, 7, 9, 1, 1, 3, 3, 7, 7, 9, 9 };
  14. int ar[12000] ;
  15. int sz,numOfLastDigits;
  16. int distances[10];
  17. void showDistances(void);
  18. void match1(int anchor);
  19. void houseKeeping(void);
  20. void windUp(void);
  21. void writeDistances(int anch);
  22. void readLastDigitsFileIntoArray(void);
  23. int size;
  24. int newDistCounter = 0;
  25. FILE *fptr1,*f1,*f3,*f7,*f9, *myFile;
  26.  
  27. int main () {
  28.  
  29. houseKeeping();
  30. readLastDigitsFileIntoArray();
  31. // int ar[numOfLastDigits];
  32. // size = sizeof (ar) / sizeof (ar[0]);
  33. // printf("\n sizeof(ar) is %d and sizeof(ar[0]) is %d \n ",sizeof(ar),sizeof(ar[0]));
  34. size = numOfLastDigits;
  35. printf("\n size = %d \n",size);
  36. for (int i =0;i<size-3;i++) {
  37. match1(i);
  38. showDistances();
  39. printf("-%d -",i);
  40. }
  41. return 0;
  42. windUp();
  43. }
  44.  
  45.  
  46. void match1(int anchor) {
  47. for(int l =0;l<10;l++) distances[l]=0;
  48. int i = anchor;
  49. int j = 1;
  50. newDistCounter=0;
  51. while ((i + j < size) && (newDistCounter<4) ){
  52. if (distances[ar[i + j]] == 0) {
  53. distances[ar[i + j]] = j;
  54. newDistCounter++;
  55. }
  56. j++;
  57. }
  58. writeDistances(ar[anchor]);
  59. }
  60.  
  61. void showDistances(void) {
  62. for (int k = 0; k < 10; k++) {
  63. printf ("%d ", distances[k]); //init the places where distances will be recorded
  64. }
  65. printf(".....\n");
  66. }
  67.  
  68. void writeDistances(int anch) {
  69. switch (anch) {
  70. case 1:
  71. fprintf(f1,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
  72. break;
  73. case 3:
  74. fprintf(f3,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
  75. break;
  76. case 7:
  77. fprintf(f7,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
  78. break;
  79. case 9:
  80. fprintf(f9,"%d %d %d %d\n",distances[1],distances[3],distances[7],distances[9]);
  81. break;
  82. default:
  83. printf("Not a prime\n");
  84. }
  85. }
  86.  
  87. void houseKeeping(void) {
  88. int status;
  89. status = remove("distFile1.txt");
  90. f1 = fopen("distFile1.txt","a");
  91. status = remove("distFile3.txt");
  92. f3 = fopen("distFile3.txt","a");
  93. status = remove("distFile7.txt");
  94. f7 = fopen("distFile7.txt","a");
  95. status = remove("distFile9.txt");
  96. f9 = fopen("distFile9.txt","a");
  97. }
  98. void windUp(void) {
  99. fclose(f1);
  100. fclose(f3);
  101. fclose(f7);
  102. fclose(f9);
  103. }
  104. void readLastDigitsFileIntoArray(void) {
  105.  
  106.  
  107. // FILE *myFile;
  108. myFile = fopen("randoms10k1379A.txt", "r");
  109. printf("Starting to insert txt file into array called numberArray[] \n");
  110. fseek(myFile, 0L, SEEK_END);
  111. sz = ftell(myFile);
  112. numOfLastDigits = sz/3;
  113. rewind(myFile);
  114. printf("The size of file is: %d bytes\n ",sz);
  115. printf("So assuning number of last digits is: %d \n ",numOfLastDigits);
  116. //read file into array
  117. int big =16;
  118. int numberArray[sz];
  119. int i;
  120.  
  121.  
  122. if (myFile == NULL){
  123. printf("Error Reading File\n");
  124. exit (0);
  125. }
  126.  
  127. for (i = 0; i < numOfLastDigits; i++){
  128. //for (i = 0; i < 16; i++){
  129. fscanf(myFile, "%d,", &numberArray[i] );
  130. printf(" %d %d \n", i,numberArray[i]);
  131. }
  132.  
  133. /* for (i = 0; i < 16; i++){
  134. printf("Number is: %d\n\n", numberArray[i]);
  135. } */
  136. printf("done, index is %d \n",i);
  137. for (i=0;i< numOfLastDigits;i++) {
  138. //for (i=0;i< 10;i++) {
  139. ar[i] = numberArray[i];
  140. //ar[i] = numberArray[i];
  141. }
  142. fclose(myFile);
  143.  
  144. // return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment