Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mutex>
- #include <thread>
- #include <vector>
- #include <utility>
- #include <cmath>
- #include <algorithm>
- #include <functional>
- using namespace std;
- bool Fix_Output = true;
- template <class T>
- using vectvect = vector <vector <T> >;
- template <class U>
- using vectvectIter = typename vector <vector <U> > ::iterator;
- vector <int> short_list;
- bool short_prime_test (int testn) {
- int maxn = 0;
- bool isprime = true;
- if (testn > 2) maxn = sqrt (testn); else maxn = 2;
- for (int test = 0; short_list [test] <= maxn && test < short_list.size (); ++test) {
- if (testn % short_list [test] == 0) isprime = false;
- }
- return isprime;
- }
- bool is_prime (int testn) {
- bool isprime = true;
- if (short_prime_test (testn) && testn > 2) {
- for (int test = 2; test < sqrt (testn); ++test) {
- if (testn % test == 0) isprime = false;
- }
- } else isprime = false;
- return isprime;
- }
- void fix_dupes (vector <int>& in) {
- vector <int> dupes;
- for (int check = 0; check < in.size () - 1; ++check) {
- if (in [check] == in [check +1])
- dupes.push_back (check);
- }
- for (int tdelete = dupes.size () -1; tdelete >= 0; --tdelete) {
- in.erase (in.begin() + dupes [tdelete]);
- }
- }
- void gen_short_list (int start, int end) {
- if (start % 2 == 0 && start > 0) --start;
- for (int find = start; find <= end; find += 2) {
- bool isprime = true;
- for (int test = 2; test < sqrt (test); ++test) {
- if (find % test == 0) isprime = false;
- }
- if (isprime) short_list.push_back (find);
- }
- }
- void findprimes (int start, int end, vectvect <int>& in, vectvectIter <int> inIter) {
- if (start % 2 == 0 && start > 0) --start;
- for (int find = start; find <= end; find += 2) {
- if (is_prime (find)) {
- inIter->push_back (find);
- }
- }
- }
- void check_input (int& min, int& max) {
- if (min < 2) {
- min = 2;
- cout << "Range exceeded. Min set to 2\n";
- }
- if (max > INT_MAX) {
- max = INT_MAX;
- cout << "Range exceeded. Max set to " << INT_MAX << endl;
- }
- cout << min << " - " << max << endl;
- }
- void combine (vectvect <int>& in, vector <int>& out) {
- for (vector <vector <int > > ::iterator tout = in.begin (); tout != in.end (); ++tout) {
- for (vector <int> ::iterator tin = tout->begin (); tin != tout->end (); ++tin) {
- out.push_back (*tin);
- }
- }
- }
- bool sortLtH (const int lv, const int rv) { return lv < rv; }
- int main () {
- cout << "How many threads?\n";
- int nthreads = 0; cin >> nthreads;
- int min = 3, max = 0;
- cout << "Find all the prime numbers between\n\n" << min << " and X: ";
- cin >> max; cout << endl;
- check_input (min, max);
- double stime = time (0); //Timer start
- vector <pair <int, int> > ranges;
- int chunk = (max - min) / nthreads;
- for (int create = 0; create < nthreads; ++create) {
- pair <int, int> tpair (0, 0);
- if (create == 0) {
- tpair.first = min;
- tpair.second = tpair.first + chunk;
- } else {
- tpair.first = ranges [create - 1].first + chunk;
- tpair.second = ranges [create - 1].second + chunk;
- }
- ranges.push_back (tpair);
- }
- gen_short_list (ranges [0].first, ranges [0].second);
- vector <thread> procs;
- vectvect <int> pieces;
- for (int thr = 0; thr < nthreads; ++thr) {
- vector <int> temp;
- pieces.push_back (temp);
- cout << "G" << thr << endl;
- vectvectIter <int> cit = pieces.begin () + thr;
- procs.push_back (thread (findprimes, ranges [thr].first, ranges [thr].second, ref (pieces), cit));
- }
- cout << "Finding..." << endl;
- int reps = max - min;
- double done = 0;
- for (int tjoin = 0; tjoin < nthreads; ++tjoin) {
- done += ((double) chunk / (double) reps) * 100;
- procs [tjoin].join ();
- cout << done << "%" << endl;
- }
- cout << "Done. Sorting...\n";
- vector <int> combined;
- combine (pieces, combined);
- if (Fix_Output) {
- sort (combined.begin (), combined.end (), sortLtH);
- fix_dupes (combined);
- }
- for (int& disp : combined)
- cout << disp << " ";
- cout << endl << endl;
- double etime = time (0) - stime; //Timer stop
- double rate = (double) reps / etime; rate /= 60;
- cout << "Processed " << rate << " per minute" << endl;
- cout << "It took " << etime / 60 << " minutes ( " << etime << " seconds )" << endl;
- cout << "\nFound: " << combined.size () << endl;
- cout << "\nDone\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment