Guest User

Untitled

a guest
Dec 19th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <gmp.h>
  3. #include <string.h>
  4.  
  5. static int _nthp[100000];
  6.  
  7. int nthp(int n) {
  8. mpz_t p;
  9. if (_nthp[n] != 0)
  10. return _nthp[n];
  11.  
  12. mpz_init_set_ui(p, 1);
  13. for (int i = 0; i < n; ++i)
  14. mpz_nextprime(p, p);
  15. int q = mpz_get_ui(p);
  16. mpz_clear(p);
  17. return ((_nthp[n] = q));
  18. }
  19.  
  20. void fp(mpz_t z, int n) {
  21. mpz_set_ui(z, 1);
  22. for (int i = 1; i <= n; ++i) {
  23. int x = i + nthp(i);
  24. mpz_mul_ui(z, z, x);
  25. }
  26. }
  27.  
  28. int divt(int n) {
  29. mpz_t z, f;
  30. mpz_init(z);
  31. mpz_init(f);
  32.  
  33. fp(z, n);
  34.  
  35. mpz_set_ui(f, 1);
  36. mpz_t p;
  37. mpz_init_set_ui(p, 1);
  38. while (1) {
  39. mpz_nextprime(p, p);
  40. if (mpz_get_ui(p) > n)
  41. break;
  42. mpz_mul(f, f, p);
  43. }
  44. mpz_clear(p);
  45.  
  46. mpq_t r;
  47. mpq_init(r);
  48. mpq_set_num(r, z);
  49. mpq_set_den(r, f);
  50. mpq_canonicalize(r);
  51.  
  52. int d = mpz_divisible_p(z, f);
  53.  
  54. // printf("%d\t", n);
  55. // mpz_out_str(stdout, 10, mpq_denref(r));
  56. // printf("\n");
  57.  
  58. mpq_clear(r);
  59. mpz_clear(z);
  60. mpz_clear(f);
  61.  
  62. return d;
  63. }
  64.  
  65. int main() {
  66. setbuf(stdout, NULL);
  67. memset(_nthp, 0, sizeof(_nthp));
  68. for (int i = 1; i < 100000; ++i) {
  69. printf("%d\t%d\n", i, divt(i));
  70. }
  71. }
Add Comment
Please, Sign In to add comment