Guest User

Untitled

a guest
May 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. 1.
  2. #include <cstdlib>
  3. 2.
  4. #include <cstdio>
  5. 3.
  6. #include <cstring>
  7. 4.
  8. #include <iostream>
  9. 5.
  10. #include <cmath>
  11. 6.
  12.  
  13. 7.
  14.  
  15. 8.
  16. #include "gmp.h"
  17. 9.
  18. #include <gmpxx.h>
  19. 10.
  20.  
  21. 11.
  22. using namespace std;
  23. 12.
  24.  
  25. 13.
  26.  
  27. 14.
  28.  
  29. 15.
  30. void pollard(mpz_t n, int c)
  31. 16.
  32. {
  33. 17.
  34. int k = 1, l = 1;
  35. 18.
  36.  
  37. 19.
  38. mpz_t g, t1, t2;
  39. 20.
  40. mpz_init (g);
  41. 21.
  42. mpz_init (t1);
  43. 22.
  44. mpz_init (t2);
  45. 23.
  46.  
  47. 24.
  48. mpz_t x, x1, P, a;
  49. 25.
  50. mpz_init_set_si (a, c);
  51. 26.
  52. mpz_init_set_si (x, 2);
  53. 27.
  54. mpz_init_set_si (x1, 2);
  55. 28.
  56.  
  57. 29.
  58. mpz_init_set_ui (P, 1);
  59. 30.
  60.  
  61. 31.
  62.  
  63. 32.
  64. while(mpz_cmp_ui(n, 1) != 0)
  65. 33.
  66. {
  67. 34.
  68. while(true)
  69. 35.
  70. {
  71. 36.
  72. mpz_mul (x, x, x);
  73. 37.
  74. mpz_add (x, x, a);
  75. 38.
  76. mpz_mod (x, x, n);
  77. 39.
  78.  
  79. 40.
  80. mpz_sub (t1, x1, x);
  81. 41.
  82. mpz_mul (t2, P, t1);
  83. 42.
  84. mpz_mod (P, t2, n);
  85. 43.
  86.  
  87. 44.
  88. if(--k > 0) continue;
  89. 45.
  90.  
  91. 46.
  92. mpz_gcd (g, P, n);
  93. 47.
  94. if(mpz_cmp_ui(g, 1) != 0) break;
  95. 48.
  96.  
  97. 49.
  98. mpz_set(x1, x);
  99. 50.
  100. k = l;
  101. 51.
  102. l *= 2;
  103. 52.
  104. for(int i = 0; i < k; i++)
  105. 53.
  106. {
  107. 54.
  108. mpz_mul (x, x, x);
  109. 55.
  110. mpz_add (x, x, a);
  111. 56.
  112. mpz_mod (x, x, n);
  113. 57.
  114. }
  115. 58.
  116. }
  117. 59.
  118.  
  119. 60.
  120. mpz_div (n, n, g);
  121. 61.
  122.  
  123. 62.
  124. if (!mpz_probab_prime_p (g, 3))
  125. 63.
  126. {
  127. 64.
  128. while(true)
  129. 65.
  130. {
  131. 66.
  132. mp_limb_t randomNr;
  133. 67.
  134. mpn_random (&randomNr, (mp_size_t)1);
  135. 68.
  136. c = (int)randomNr;
  137. 69.
  138. if(c != 2 && c != 0) break;
  139. 70.
  140. }
  141. 71.
  142. pollard(g, c);
  143. 72.
  144. }
  145. 73.
  146. else
  147. 74.
  148. gmp_printf("%Zd\n", g);
  149. 75.
  150.  
  151. 76.
  152. if(mpz_probab_prime_p (n, 3))
  153. 77.
  154. {
  155. 78.
  156. gmp_printf("%Zd\n", n);
  157. 79.
  158. break;
  159. 80.
  160. }
  161. 81.
  162. }
  163. 82.
  164.  
  165. 83.
  166. mpz_clear (g);
  167. 84.
  168. mpz_clear (P);
  169. 85.
  170. mpz_clear (t2);
  171. 86.
  172. mpz_clear (t1);
  173. 87.
  174. mpz_clear (a);
  175. 88.
  176. mpz_clear (x1);
  177. 89.
  178. mpz_clear (x);
  179. 90.
  180. }
  181. 91.
  182.  
  183. 92.
  184. int main (int argc, char *argv[])
  185. 93.
  186. {
  187. 94.
  188.  
  189. 95.
  190. mpz_t tt;
  191. 96.
  192. mpz_init(tt);
  193. 97.
  194. while(gmp_scanf("%Zd", tt))
  195. 98.
  196. {
  197. 99.
  198. pollard(tt, 1);
  199. 100.
  200. printf("\n");
  201. 101.
  202. }
  203. 102.
  204.  
  205. 103.
  206.  
  207. 104.
  208. return 0;
  209. 105.
  210. }
  211. 106.
Add Comment
Please, Sign In to add comment