Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "is_prime.h"
- #include <cmath>
- #include <iostream>
- #include <algorithm>
- #include <thread>
- int threads = std::thread::hardware_concurrency();
- void work_IsPrime(const uint64_t& x,int index,bool* is_some_comp){
- uint64_t root = sqrt(x);
- auto bound = std::min(root + 6, x);
- for (auto i = 2ull + index; i < bound; i += threads) {
- if(*is_some_comp){
- return;
- }
- if (x % i == 0) {
- *is_some_comp = true;
- break;
- }
- }
- }
- bool IsPrime(uint64_t x) {
- if (x <= 1) {
- return false;
- }
- std::vector<std::thread> workers;
- bool is_some_comp = false;
- for (unsigned i = 0; i < threads; ++i) {
- workers.emplace_back(work_IsPrime, std::cref(x), i,&is_some_comp);
- }
- for (std::thread& t : workers) {
- t.join();
- }
- if(is_some_comp){
- return false;
- }else{
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement