Advertisement
Art_Uspen

Untitled

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