Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <random>
- #include <boost/multiprecision/cpp_int.hpp>
- #include <boost/multiprecision/gmp.hpp>
- #include <boost/multiprecision/cpp_int/literals.hpp>
- #include <boost/math/constants/constants.hpp>
- #include <functional>
- #include <chrono>
- #include <random>
- #include <iostream>
- #include <thread>
- #include <map>
- using namespace std;
- using namespace boost::multiprecision;
- using namespace boost::multiprecision::literals;
- class stoper
- {
- chrono::time_point<chrono::high_resolution_clock> a,b;
- public:
- void start(){a = chrono::high_resolution_clock::now();}
- void stop() {b = chrono::high_resolution_clock::now();}
- double value()
- {
- chrono::duration<double> elapsed_seconds = b-a;
- return elapsed_seconds.count();
- }
- void show()
- {
- cout << value()<<"s "<<endl;
- }
- };
- template <typename T>
- void factor( T x)
- {
- while ((x % 2) == 0) {
- x /= 2;
- std::cout << 2 << " ";
- }
- T i = 3;
- T e = sqrt(x);
- while (i <= e) {
- while ((x % i) == 0) {
- x /= i;
- e = sqrt(x);
- std::cout << i << " ";
- }
- i+=2;
- }
- if (x > 1)
- std::cout << x << " ";
- }
- template <typename T>
- void factor2( T x)
- {
- while ((x % 2) == 0) {
- x /= 2;
- std::cout << 2 << " ";
- }
- T i = 3;
- T e = sqrt(x);
- while (i <= e) {
- T q,r;
- divide_qr(x,i,q,r);
- while (r== 0) {
- x = q;
- divide_qr(x,i,q,r);
- std::cout << i << " ";
- e = sqrt(x);
- }
- i+=2;
- }
- if (x > 1)
- std::cout << x << " ";
- }
- template <typename T>
- void factor3( T x)
- {
- while ((x % 2) == 0) {
- x /= 2;
- std::cout << 2 << " ";
- }
- T i = 3;
- T e = sqrt(x);
- while (i <= e) {
- T q,r;
- bool change=false;
- divide_qr(x,i,q,r);
- while (r== 0) {
- x = q;
- divide_qr(x,i,q,r);
- std::cout << i << " ";
- change=true;
- }
- if (change) e = sqrt(x);
- i+=2;
- }
- if (x > 1)
- std::cout << x << " ";
- }
- template <typename T, typename F >
- void test( F fun )
- {
- T a("5742507205757425072053");
- stoper s;
- s.start();
- fun(a);
- s.stop();
- s.show();
- }
- int main()
- {
- test<cpp_int>(factor<cpp_int>);
- test<mpz_int>(factor<mpz_int>);
- test<cpp_int>(factor2<cpp_int>);
- test<mpz_int>(factor2<mpz_int>);
- test<cpp_int>(factor3<cpp_int>);
- test<mpz_int>(factor3<mpz_int>);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement