1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <vector>
#include <thread>
#include <functional>
void count_prime(int nStart, int nEnd, std::vector<uint64_t>& temp, int id);
int main()
{
int nPrime;
std::cin >> nPrime;
std::vector<uint64_t> prime_batch_1, prime_batch_2;
int n_prime_1, n_prime_2;
if (nPrime % 2 == 0) //checks if nPrime is even
{
n_prime_1 = nPrime / 2;
n_prime_2 = nPrime / 2;
}
else //if nPrime is not even
{
n_prime_1 = nPrime / 2;
n_prime_2 = (nPrime / 2) + 1;
}
std::thread first_part( count_prime, 2, n_prime_1, std::ref( prime_batch_1 ) , 1 );
std::thread second_part( count_prime, n_prime_2, nPrime, std::ref( prime_batch_2 ), 2 );
first_part.join();
second_part.join();
for (int i = 0; i < prime_batch_1.size(); ++i)
{
std::cout << prime_batch_1.at( i );
}
for (int i = 0; i < prime_batch_2.size(); ++i)
{
std::cout << prime_batch_2.at( i );
}
}
void count_prime(int nStart, int nEnd, std::vector<uint64_t>& temp, int id)
{
bool isPrime;
for (int i = nStart; i <= nEnd; ++i)
{
std::cout << "hello from thread : " << id << " at : " << i << "\n";
isPrime = true;
if (i == 2);
if (i % 2 == 0)
{
isPrime = false;
goto end_label;
}
else
{
for (int n = 0; n <= i / 2; ++n)
{
if (i % n == 0)
{
isPrime = false;20
goto end_label;
}
}
}
temp.push_back(i);
end_label :;
}
}
|