Advertisement
Art_Uspen

Untitled

Nov 18th, 2021
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include "is_prime.h"
  2. #include <cmath>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <thread>
  6.  
  7. int threads = std::thread::hardware_concurrency();
  8.  
  9. void work_IsPrime(const uint64_t& x,int index,bool* is_some_comp){
  10.     uint64_t root = sqrt(x);
  11.     auto bound = std::min(root + 6, x);
  12.     for (auto i = 2ull + index; i < bound; i += threads) {
  13.         if(*is_some_comp){
  14.             return;
  15.         }
  16.         if (x % i == 0) {
  17.             *is_some_comp = true;
  18.             break;
  19.         }
  20.     }
  21. }
  22. bool IsPrime(uint64_t x) {
  23.     if (x <= 1) {
  24.         return false;
  25.     }
  26.     std::vector<std::thread> workers;
  27.    bool is_some_comp = false;
  28.     for (unsigned i = 0; i < threads; ++i) {
  29.         workers.emplace_back(work_IsPrime, std::cref(x), i,&is_some_comp);
  30.     }
  31.     for (std::thread& t : workers) {
  32.         t.join();
  33.     }
  34.     if(is_some_comp){
  35.         return false;
  36.     }else{
  37.         return true;
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement