Guest User

Untitled

a guest
Jul 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <time.h>
  5.  
  6. typedef unsigned long long int ULLONG;
  7.  
  8. ULLONG * getprimes(ULLONG upper) {
  9. ULLONG *results, *tmp, current_size = upper / 3, n = 0, i, j;
  10. unsigned char * pool;
  11.  
  12. pool = malloc(sizeof(unsigned char) * upper);
  13. if (!pool)
  14. return NULL;
  15.  
  16. memset(pool, (unsigned char) 1, upper);
  17.  
  18. for (i = 4; i < upper; i += 2)
  19. pool[i] = 0;
  20.  
  21. for (i = 0; i < upper; i++) {
  22. if (i % 2 == 0)
  23. continue;
  24. for (j = (i + 2) * 2; j < upper; j += (i + 2)) {
  25. pool[j] = 0;
  26. }
  27. }
  28.  
  29. results = malloc(sizeof(ULLONG) * current_size + 1);
  30. if (!results)
  31. return NULL;
  32.  
  33. for (i = 2; i < upper; i++) {
  34. if (pool[i]) {
  35. if (n >= current_size) {
  36. current_size *= 2;
  37. tmp = realloc(results, sizeof(ULLONG) * current_size);
  38. if (!tmp) {
  39. return NULL;
  40. }
  41. results = tmp;
  42. }
  43. results[n++] = i;
  44. }
  45. }
  46. results[n] = 0;
  47. return results;
  48. }
  49.  
  50. int main(int argc, char *argv[]) {
  51. /*ULLONG *results;
  52. clock_t before, after;
  53. double duration;
  54.  
  55. before = clock();
  56. results = getprimes(200000);
  57. after = clock();
  58. duration = (double)(after - before) / CLOCKS_PER_SEC;
  59. printf("%f\n", duration);
  60. free(results);
  61.  
  62. before = clock();
  63. results = getprimes(2000000);
  64. after = clock();
  65. duration = (double)(after - before) / CLOCKS_PER_SEC;
  66. printf("%f\n", duration);
  67. free(results);
  68.  
  69. before = clock();
  70. results = getprimes(200000000);
  71. after = clock();
  72. duration = (double)(after - before) / CLOCKS_PER_SEC;
  73. printf("%f\n", duration);
  74. free(results);
  75.  
  76. /*while (*results) {
  77. printf("%lld ", *results);
  78. results++;
  79. }
  80. puts("\n");*/
  81. ULLONG target = 600851475143, upper = 1000;
  82. ULLONG *factors, *tmp;
  83. ULLONG *primes = getprimes(upper);
  84. int found = 0, n = 0, current_size = 10, i;
  85.  
  86. factors = malloc(sizeof(unsigned char) * current_size);
  87.  
  88. while (target > 2) {
  89. while (*primes) {
  90. if (target % *primes == 0) {
  91. found = 1;
  92.  
  93. if (n >= current_size) {
  94. current_size *= 2;
  95. tmp = realloc(factors, sizeof(ULLONG) * current_size);
  96. if (!tmp) {
  97. exit(1);
  98. }
  99. factors = tmp;
  100. }
  101.  
  102. printf("%lld\n", target);
  103. printf("%lld\n", *primes);
  104. factors[n++] = *primes;
  105. target /= *primes;
  106. break;
  107. }
  108. primes++;
  109. }
  110.  
  111. if (!found) {
  112. if (target > upper) {
  113. upper *= 2;
  114. free(primes);
  115. primes = getprimes(upper);
  116. } else {
  117. printf("%lld\n", target);
  118. break;
  119. }
  120. }
  121. }
  122.  
  123. /*for (i = 0; i < n; i++)
  124. printf("%lld\n", factors[i]);*/
  125.  
  126.  
  127. }
Add Comment
Please, Sign In to add comment