Advertisement
Glenpl

simplyfying roots

Jun 5th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4. #include <utility>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class Root
  10. {
  11. private:
  12.     std::vector<pair<unsigned int, unsigned int>> base;
  13.     unsigned int root;
  14.     unsigned int multipliquer;
  15.     unsigned int root_degree;
  16.  
  17.     void simplyfy();
  18.  
  19. public:
  20.     Root(unsigned int root, unsigned int root_degree = 2)
  21.         : root(root), root_degree(root_degree)
  22.     {
  23.         unsigned int max = pow(4294967294, 1.0/static_cast<double>(root_degree));
  24.         for(unsigned int i = 2; i < max; i++)
  25.             base.push_back(make_pair(i, pow(i, root_degree)));
  26.  
  27.         std::reverse(base.begin(), base.end());
  28.  
  29.         multipliquer = 0;
  30.         this -> simplyfy();
  31.     }
  32.  
  33.     void result();
  34. };
  35.  
  36. void Root::simplyfy()
  37. {
  38.     for(int i = 0; i < 3; i++)
  39.     {
  40.         for(auto x: base)
  41.             if( root == x.second )
  42.             {
  43.                 root = 1;
  44.                 multipliquer += x.first;
  45.                 return;
  46.             }
  47.  
  48.         for(auto x: base)
  49.             if( root > 1 && root % x.second == 0 )
  50.             {
  51.                 root /= x.second;
  52.                 multipliquer += x.first;
  53.             }
  54.     }
  55. }
  56.  
  57. void Root::result()
  58. {
  59.     if ( multipliquer == 0 )
  60.         cout << root_degree << "th degree root of " << root << "\n";
  61.     else if ( root == 1 )
  62.         cout << multipliquer << "\n";
  63.     else
  64.         cout << multipliquer << " * root(" << root << ") (" << root_degree << "th degree)\n";
  65. }
  66.  
  67. int main()
  68. {
  69.     unsigned int root, root_degree;
  70.     while(cin >> root >> root_degree)
  71.     {
  72.         Root* x = new Root(root, root_degree);
  73.         x -> result();
  74.         cout << "\n\n>";
  75.         delete x;
  76.     }
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement