-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtbb_factor.cpp
More file actions
57 lines (44 loc) · 893 Bytes
/
Copy pathtbb_factor.cpp
File metadata and controls
57 lines (44 loc) · 893 Bytes
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
#include <iostream>
#include <math.h>
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
typedef unsigned long long num_t;
struct FactorWorker
{
num_t n;
num_t *res;
FactorWorker(num_t _n, num_t *_res)
{
n=_n;
res=_res;
}
void operator()(num_t i) const
{
if( (n%i) ==0){ // found a factor
*res=i; // record the factor
}
}
};
num_t ParallelFactor(num_t n)
{
num_t mx=1+ceil(sqrt((double)n));
num_t res=n;
tbb::parallel_for((num_t)2, mx,
// body of the for loop
FactorWorker(n,&res)
);
return res;
}
int main(int argc, char *[])
{
num_t numerator;
while(true){
std::cin>>numerator;
if(std::cin.fail())
break;
num_t seqResult=ParallelFactor(numerator);
std::cout<<"Numerator = "<<numerator<<"\n";
std::cout<<" ParallelFactor = "<<seqResult<<"\n";
}
return 0;
}