使用线程计算素数
Calculate prime numbers using threads
我必须通过线程计算范围之间的素数。示例程序运行
./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;
}
只需对每个范围中的每个值使用Rabin-Miller素性测试,就可以实现这一点。Rabbin-Miller算法不依赖于先前值的素性计算,因此所有线程都可以在没有任何通信的情况下孤立运行。
参见:
- https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test
- 算法简介
相关文章:
- 我可以计算多线程数的平均值吗?
- 用于在C++中计算用户数据的线程
- 在具有许多内核的计算机上,使用 Boost ASIO 只能使用 1 个线程
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- 提升线程:术语的计算结果不为 0 个参数
- 从多个线程组随机计算着着色器写入RWStructuredBuffer
- 多线程蒙特卡罗计算中没有加速
- 使用C 中的线程并行计算
- 在 C++11 中计算字母和单词的双字母组合的 std::线程向量的问题
- 多线程计算均值和std并不能提高效率
- 我遇到了一个关于多线程的小问题.需要多线程来计算 Pi 和方差
- 多个线程创建5个线程来计算质数
- C++STL多线程,并行运行计算
- 线程:如何在C或C++中精确计算算法的执行时间(函数的持续时间)
- qt多线程:计算一个积分
- 计算创建 n 个线程所花费的时间
- 使用C++中的线程来报告计算进度
- 使用多线程计算质数
- 计算线程运行的次数
- 正在计算线程内的时间