Advertisement
bartekltg

cpp_int gmp naiwny rozklad

Sep 30th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <boost/multiprecision/cpp_int.hpp>
  4. #include <boost/multiprecision/gmp.hpp>
  5. #include <boost/multiprecision/cpp_int/literals.hpp>
  6. #include <boost/math/constants/constants.hpp>
  7. #include <functional>
  8.  
  9.  
  10.  
  11. #include <chrono>
  12. #include <random>
  13. #include <iostream>
  14. #include <thread>
  15. #include <map>
  16.  
  17.  
  18. using namespace std;
  19.  
  20. using namespace boost::multiprecision;
  21. using namespace boost::multiprecision::literals;
  22.  
  23.  
  24. class stoper
  25. {
  26.     chrono::time_point<chrono::high_resolution_clock> a,b;
  27. public:
  28.     void start(){a = chrono::high_resolution_clock::now();}
  29.     void stop() {b = chrono::high_resolution_clock::now();}
  30.     double value()
  31.     {
  32.         chrono::duration<double> elapsed_seconds = b-a;
  33.         return elapsed_seconds.count();
  34.     }
  35.     void show()
  36.     {
  37.         cout << value()<<"s "<<endl;
  38.     }
  39. };
  40.  
  41.  
  42. template <typename T>
  43. void factor( T x)
  44. {
  45.     while ((x % 2) == 0) {
  46.         x /= 2;
  47.         std::cout << 2 << " ";
  48.     }
  49.     T i = 3;
  50.     T e = sqrt(x);
  51.     while (i <= e) {
  52.         while ((x % i) == 0) {
  53.             x /= i;
  54.             e = sqrt(x);
  55.             std::cout << i << " ";
  56.         }
  57.         i+=2;
  58.     }
  59.     if (x > 1)
  60.         std::cout << x << " ";
  61. }
  62.  
  63. template <typename T>
  64. void factor2( T x)
  65. {
  66.     while ((x % 2) == 0) {
  67.         x /= 2;
  68.         std::cout << 2 << " ";
  69.     }
  70.     T i = 3;
  71.     T e = sqrt(x);
  72.     while (i <= e) {
  73.         T q,r;
  74.         divide_qr(x,i,q,r);
  75.         while (r== 0) {
  76.             x = q;
  77.             divide_qr(x,i,q,r);
  78.             std::cout << i << " ";
  79.             e = sqrt(x);
  80.         }
  81.         i+=2;
  82.     }
  83.     if (x > 1)
  84.         std::cout << x << " ";
  85. }
  86.  
  87.  
  88. template <typename T>
  89. void factor3( T x)
  90. {
  91.     while ((x % 2) == 0) {
  92.         x /= 2;
  93.         std::cout << 2 << " ";
  94.     }
  95.     T i = 3;
  96.     T e = sqrt(x);
  97.     while (i <= e) {
  98.         T q,r;
  99.         bool change=false;
  100.         divide_qr(x,i,q,r);
  101.         while (r== 0) {
  102.             x = q;
  103.             divide_qr(x,i,q,r);
  104.             std::cout << i << " ";
  105.             change=true;
  106.         }
  107.         if (change) e = sqrt(x);
  108.         i+=2;
  109.     }
  110.     if (x > 1)
  111.         std::cout << x << " ";
  112. }
  113.  
  114.  
  115. template <typename T, typename F >
  116. void test( F fun  )
  117. {
  118.     T a("5742507205757425072053");
  119.     stoper s;
  120.     s.start();
  121.     fun(a);
  122.     s.stop();
  123.     s.show();
  124. }
  125.  
  126. int main()
  127. {
  128.     test<cpp_int>(factor<cpp_int>);
  129.     test<mpz_int>(factor<mpz_int>);
  130.  
  131.     test<cpp_int>(factor2<cpp_int>);
  132.     test<mpz_int>(factor2<mpz_int>);
  133.  
  134.     test<cpp_int>(factor3<cpp_int>);
  135.     test<mpz_int>(factor3<mpz_int>);
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement