使用线程计算素数

时间:2015-06-24 09:29:56

标签: c++ multithreading

我必须按线程计算范围之间的素数。 示例程序运行

./myProg 100 250 4 //4 number of threads and 100 to 250 is range

我做了基本的部分,下一步是什么?

#include<iostream>
#include<pthread.h>
#include<string>
#include<cstdlib>

using namespace std;

void* prime(void*);//calculate prime number for this thread between range x to y ???

int main(int argc, char** argv){

    if(argc!=4){
        cout<<"Must provide exactly 3 arguments"<<endl;
        exit(EXIT_FAILURE);
        }

    pthread_t threads[(*argv[3])];

    cout<<"Prime numbers will be calculated by "<<(*argv[3])<<" threads"<<endl;



    return 0;
}

1 个答案:

答案 0 :(得分:0)

你可以通过对每个范围内的每个值使用Rabin-Miller素性测试来做到这一点。 Rabbin-Miller算法不依赖于先前值的素数计算,因此所有线程都可以隔离运行而不需要它们之间的任何通信。

请参阅:

相关问题