google-billboard.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <inttypes.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. /*
  5. * Function Prototypes
  6. */
  7. int isPrime(uint64_t);
  8. int isNumeric(char);
  9. int isFullLength(char[]);
  10. uint64_t arrToNum(char[], int);
  11. void modBuff(char*, int);
  12. /*
  13. * Main Method
  14. */
  15. int main(int argc, char **argv) {
  16. /* Open the site as a file for reading the source */
  17. FILE *fptr = popen("wget --quiet -O - http://www-history.mcs.st-andrews.ac.uk/HistTopics/e_10000.html", "r");
  18. /* Variables --------------------------------------------------------- */
  19. int ctr = 1, itr; /* Two counter variables for the for loop and count */
  20. char buf[10]; /* A character buffer to store the individual digits */
  21. char cptr; /* A character to hold the next item in the stream */
  22. uint64_t num; /* An unsigned 64-bit integer to hold the number */
  23. /* ------------------------------------------------------------------- */
  24. /* Move the pointer to the numbers past the decimal point */
  25. while ((cptr = fgetc(fptr)) != '.');
  26. /* Put the first 10-digit number in the buffer */
  27. for (itr = 0; itr < 10; itr++) {
  28. /* Grab the next character */
  29. cptr = fgetc(fptr);
  30. /* Check to see if the character is numeric */
  31. if (isNumeric(cptr)) {
  32. /* Store it in the buffer if it is */
  33. buf[itr] = cptr;
  34. } else {
  35. /* Peel off any non-numeric characters */
  36. while (!isNumeric(cptr)) cptr = fgetc(fptr);
  37. /* Assign the character to the buffer if it's numeric */
  38. if (isNumeric(cptr)) buf[itr] = cptr;
  39. }
  40. }
  41. /* Iterate across the data and test the numbers */
  42. do {
  43. /* Check that the 10-digit number doesn't actually start with a 0,
  44. * and skip this iteration if it does. */
  45. if (isFullLength(buf)) {
  46. /* Convert the buffer into an integer */
  47. num = arrToNum(buf, 10);
  48. /* Print the number and increment the counter */
  49. printf("%02d.) %" PRIu64 "", ctr++, num);
  50. /* Check for primality of the number to exit the loop */
  51. if (isPrime(num)) {
  52. printf (" (1st 10-digit prime)\n");
  53. break;
  54. }
  55. /* Separate the numbers */
  56. printf("\n");
  57. }
  58. /* Grab the next character */
  59. cptr = fgetc(fptr);
  60. /* Peel off any non-numeric characters due to pre-formatted text */
  61. while (!isNumeric(cptr) && cptr != '<') cptr = fgetc(fptr);
  62. /* Check to make sure we're not at the end of the list */
  63. if (cptr == '<') break;
  64. /* Slide the unused numbers in the buffer down one step */
  65. modBuff(buf, 10);
  66. /* Put the next character at the end of the buffer */
  67. buf[9] = cptr;
  68. } while (isNumeric(cptr));
  69. /* Close the website pointer */
  70. pclose(fptr);
  71. /* Exit */
  72. return 0;
  73. }
  74. /*
  75. * isPrime(uint64_t)
  76. * Checks for primality of a number
  77. */
  78. int isPrime(uint64_t num) {
  79. if (num < 2) return 0;
  80. if (num == 2 || num == 3) return 1;
  81. if (num % 2 == 0) return 0;
  82. uint64_t sr = ceil(pow(num, 0.5));
  83. uint64_t ctr;
  84. for (ctr = 3; ctr <= sr; ctr += 2) if (num % ctr == 0) return 0;
  85. return 1;
  86. }
  87. /*
  88. * isNumeric(char)
  89. * Checks to make sure the character passed is a number
  90. */
  91. int isNumeric(char num) {
  92. return (num - '0' >= 0 && num - '0' <= 9);
  93. }
  94. /*
  95. * isFullLength(char[])
  96. * Simply checks to see that an n-digit number is, in fact, n-digits long
  97. */
  98. int isFullLength(char arr[]) {
  99. return (arr[0] - '0');
  100. }
  101. /*
  102. * arrToNum(char[], int)
  103. * Converts a character array to an integer
  104. */
  105. uint64_t arrToNum(char arr[], int len) {
  106. uint64_t res = 0;
  107. int ctr;
  108. for (ctr = 0; ctr < len; ctr++) res = (res * 10) + (arr[ctr] - '0');
  109. return res;
  110. }
  111. /*
  112. * modBuff(*char, int)
  113. * Slides the elements in the buffer down one slot
  114. */
  115. void modBuff(char *arr, int len) {
  116. int ctr;
  117. for (ctr = 0; ctr < (len - 1); ctr++) arr[ctr] = arr[ctr + 1];
  118. return;
  119. }