Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #include <assert.h>
  8. #define MAXPOWER 19
  9. #define MAXPRIME 100000
  10. #define BASE 10
  11.  
  12. /* this function verifies if a number nTest is a prime number, returns 1 if
  13. prime or 0 if not, it return -1 if there was a problem in the function */
  14. int primeTest (int nTest);
  15.  
  16. /* this function makes the update of char array, that prime number is prime,
  17. and the multiples of prime are composites, then it returns 1 if all ok or 0 if
  18. there was a problem in the function, 0 not processed, 1 prime, 2 not prime */
  19. int updateSievePrime(char *sievePrime, int size, int prime);
  20.  
  21. /* Iterative Function to calculate (base^exponent)%modulus in O(log exponent) */
  22. long long int modular_pow(long long int base, long long int exponent,
  23. long long int modulus);
  24.  
  25. int main () {
  26. clock_t inicio, fim;
  27. double tempo;
  28. inicio = clock();
  29. /* work to verify */
  30. char sievePrime[MAXPRIME]={'0'};
  31. int i, boolean, nPrimes = 0, *v, j, nFound = 0, answer, sumFound=0, totalSum=0,nAnswer;
  32. int step=1;
  33. long long int res;
  34. /* memory allocation */
  35. v = (int*) calloc(MAXPRIME, sizeof (int));
  36. assert(v!=NULL);
  37. for (i = 2; i < MAXPRIME; i+=step) {
  38. /* step update */
  39. if (i == 3) {
  40. step++;
  41. }
  42. /* 0 not processed, 1 prime, 2 not prime */
  43. if (sievePrime[i] == '2') {
  44. continue;
  45. }
  46. /* test not processed numbers */
  47. boolean = primeTest(i);
  48. assert(boolean!=-1);
  49. if (boolean == 1) {
  50. /* new prime found */
  51. v[nPrimes] = i;
  52. totalSum+=i;
  53. nPrimes++;
  54. boolean = updateSievePrime(sievePrime, MAXPRIME, i);
  55. assert(boolean!=0);
  56. }
  57. }
  58. /* memory reallocation */
  59. v = (int*) realloc(v, nPrimes*sizeof (int));
  60. assert(v!=NULL);
  61. /* testing primes */
  62. for (i = 0; i < nPrimes; i++) {
  63. for (j = 0; j < MAXPOWER; j++) {
  64. res = modular_pow(BASE, roundl(pow(10,j)), 9*v[i]);
  65. if (res == 1) {
  66. nFound++;
  67. sumFound+=v[i];
  68. /* no more need to further tests */
  69. break;
  70. }
  71. }
  72. }
  73. /* answer calc */
  74. answer = totalSum - sumFound;
  75. /* end of the work */
  76. fim = clock();
  77. tempo = (double)(fim - inicio) / CLOCKS_PER_SEC;
  78. printf("\nTempo em segundos: %lf", tempo);
  79. printf("\nUntil primes < %d, the total sum of all the primes that will never be\nprime of R(10^n) is %d.", MAXPRIME, answer);
  80. printf("\n");
  81. /* free memory */
  82. free(v);
  83. return 0;
  84. }
  85. /******************************************************************************/
  86. int primeTest (int nTest) {
  87. /* this function verifies if a number nTest is a prime number, returns 1 if
  88. prime or 0 if not, it return -1 if there was a problem in the function */
  89. int i, comparador;
  90. if (nTest <= 1) {
  91. return 0;
  92. }
  93. if (nTest == 2) {
  94. return 1;
  95. } else if (nTest % 2 == 0) {
  96. return 0;
  97. }
  98. for (i = 3; i < sqrt(nTest+1); i = i+2) {
  99. if (nTest % i == 0) {
  100. return 0;
  101. }
  102. }
  103. /* if it reaches here, then it is a prime number */
  104. return 1;
  105. }
  106. /******************************************************************************/
  107. int updateSievePrime(char *sievePrime, int size, int prime) {
  108. /* this function makes the update of char array, that prime number is prime,
  109. and the multiples of prime are composites, then it returns 1 if all ok or 0 if
  110. there was a problem in the function, 0 not processed, 1 prime, 2 not prime */
  111. if (sievePrime == NULL || size < 1 || prime < 2) {
  112. return 0;
  113. }
  114. int mult = 2, pos;
  115. while (1) {
  116. pos = mult*prime;
  117. if (pos>=size) {
  118. /* end array */
  119. return 1;
  120. }
  121. sievePrime[pos] = '2';
  122. mult++;
  123. }
  124. }
  125. /******************************************************************************/
  126. long long int modular_pow(long long int base, long long int exponent,
  127. long long int modulus) {
  128. /* Iterative Function to calculate (base^exponent)%modulus in O(log exponent) */
  129. if (modulus == 1) {
  130. return 0;
  131. }
  132. //Assert :: (modulus - 1) * (modulus - 1) does not overflow base
  133. long long int result = 1;
  134. base = base % modulus;
  135. while (exponent > 0){
  136. if (exponent % 2 == 1) {
  137. result = (result * base) % modulus;
  138. }
  139. exponent = exponent >> 1;
  140. base = (base * base) % modulus;
  141. }
  142. return result;
  143. }
  144. /******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement