Carcigenicate

Prime finder (Mess)

Feb 27th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <mutex>
  3. #include <thread>
  4. #include <vector>
  5. #include <utility>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <functional>
  9.  
  10. using namespace std;
  11.  
  12. bool Fix_Output = true;
  13.  
  14. template <class T>
  15. using vectvect = vector <vector <T> >;
  16. template <class U>
  17. using vectvectIter = typename vector <vector <U> > ::iterator;
  18.  
  19. vector <int> short_list;
  20.  
  21.     bool short_prime_test (int testn) {
  22.         int maxn = 0;
  23.         bool isprime = true;
  24.         if (testn > 2) maxn = sqrt (testn); else maxn = 2;
  25.         for (int test = 0; short_list [test] <= maxn && test < short_list.size (); ++test) {
  26.             if (testn % short_list [test] == 0) isprime = false;
  27.         }
  28.         return isprime;
  29.     }
  30.    
  31.     bool is_prime (int testn) {
  32.         bool isprime = true;
  33.         if (short_prime_test (testn) && testn > 2) {
  34.             for (int test = 2; test < sqrt (testn); ++test) {
  35.                 if (testn % test == 0) isprime = false;
  36.             }
  37.         } else isprime = false;
  38.         return isprime;
  39.     }
  40.    
  41. void fix_dupes (vector <int>& in) {
  42.     vector <int> dupes;
  43.     for (int check = 0; check < in.size () - 1; ++check) {
  44.         if (in [check] == in [check +1])
  45.             dupes.push_back (check);
  46.     }
  47.     for (int tdelete = dupes.size () -1; tdelete >= 0; --tdelete) {
  48.         in.erase (in.begin() + dupes [tdelete]);
  49.     }
  50. }
  51.  
  52. void gen_short_list (int start, int end) {
  53.     if (start % 2 == 0 && start > 0) --start;
  54.     for (int find = start; find <= end; find += 2) {
  55.         bool isprime = true;
  56.         for (int test = 2; test < sqrt (test); ++test) {
  57.                 if (find % test == 0) isprime = false;
  58.         }
  59.         if (isprime) short_list.push_back (find);
  60.     }
  61. }
  62.  
  63. void findprimes (int start, int end, vectvect <int>& in, vectvectIter <int> inIter) {
  64.     if (start % 2 == 0 && start > 0) --start;
  65.     for (int find = start; find <= end; find += 2) {
  66.         if (is_prime (find)) {
  67.             inIter->push_back (find);
  68.         }
  69.     }
  70. }
  71.    
  72. void check_input (int& min, int& max) {
  73.     if (min < 2) {
  74.         min = 2;
  75.         cout << "Range exceeded. Min set to 2\n";
  76.     }
  77.     if (max > INT_MAX) {
  78.         max = INT_MAX;
  79.         cout << "Range exceeded. Max set to " << INT_MAX << endl;
  80.     }
  81.     cout << min << " - " << max << endl;
  82. }
  83.  
  84. void combine (vectvect <int>& in, vector <int>& out) {
  85.     for (vector <vector <int > > ::iterator tout = in.begin (); tout != in.end (); ++tout) {
  86.         for (vector <int> ::iterator tin = tout->begin (); tin != tout->end (); ++tin) {
  87.                 out.push_back (*tin);
  88.         }  
  89.     }
  90. }
  91.    
  92. bool sortLtH (const int lv, const int rv) { return lv < rv; }
  93.  
  94.  
  95. int main () {
  96.    
  97.     cout << "How many threads?\n";
  98.     int nthreads = 0; cin >> nthreads;
  99.    
  100.     int min = 3, max = 0;
  101.     cout << "Find all the prime numbers between\n\n" << min << " and X: ";
  102.     cin >> max; cout << endl;
  103.    
  104.     check_input (min, max);
  105.    
  106.     double stime = time (0); //Timer start
  107.    
  108.     vector <pair <int, int> > ranges;
  109.     int chunk = (max - min) / nthreads;
  110.    
  111.     for (int create = 0; create < nthreads; ++create) {
  112.         pair <int, int> tpair (0, 0);
  113.         if (create == 0) {
  114.             tpair.first = min;
  115.             tpair.second = tpair.first + chunk;
  116.         } else {
  117.             tpair.first = ranges [create - 1].first + chunk;
  118.             tpair.second = ranges [create - 1].second + chunk;
  119.         }
  120.         ranges.push_back (tpair);
  121.     }
  122.    
  123.     gen_short_list (ranges [0].first, ranges [0].second);
  124.    
  125.     vector <thread> procs;
  126.     vectvect <int> pieces;
  127.     for (int thr = 0; thr < nthreads; ++thr) {
  128.         vector <int> temp;
  129.         pieces.push_back (temp);
  130.         cout << "G" << thr << endl;
  131.         vectvectIter <int> cit = pieces.begin () + thr;
  132.         procs.push_back (thread (findprimes, ranges [thr].first, ranges [thr].second, ref (pieces), cit));
  133.     }
  134.    
  135.     cout << "Finding..." << endl;
  136.    
  137.     int reps = max - min;
  138.    
  139.     double done = 0;
  140.     for (int tjoin = 0; tjoin < nthreads; ++tjoin) {
  141.         done += ((double) chunk / (double) reps) * 100;
  142.         procs [tjoin].join ();
  143.         cout << done << "%" << endl;
  144.     }
  145.     cout << "Done. Sorting...\n";
  146.    
  147.     vector <int> combined;
  148.     combine (pieces, combined);
  149.    
  150.     if (Fix_Output) {
  151.         sort (combined.begin (), combined.end (), sortLtH);
  152.         fix_dupes (combined);
  153.     }
  154.        
  155.     for (int& disp : combined)
  156.         cout << disp << " ";
  157.        
  158.     cout << endl << endl;
  159.    
  160.     double etime = time (0) - stime; //Timer stop
  161.    
  162.     double rate =  (double) reps / etime; rate /= 60;
  163.    
  164.     cout << "Processed " << rate << " per minute" << endl;
  165.    
  166.     cout << "It took " << etime / 60 << " minutes ( " << etime << " seconds )" << endl;
  167.    
  168.    
  169.     cout << "\nFound: " << combined.size () << endl;
  170.    
  171.     cout << "\nDone\n";
  172. }
Advertisement
Add Comment
Please, Sign In to add comment