#include<stdio.h>
#include<mpi.h>
#include<malloc.h>
#include<math.h>
#include<string.h>
bool jud(int a) {
int k = 0;
if (a <= 1)
return false;
for (int i = 2; i < pow(a, 0.5) + 1; i++) {
k = a % i;
if (k == 0) {
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int tot = 10000; //判断上限
int times = tot / size;
int exc = tot % size;
int num = 0;
int number = 0;
int temp_num = 0;
for (int i = 0; i < times; i++) {
temp_num = rank + size * i;
if (jud(temp_num)) {
num++;
}
}
if (rank < exc) {
int part = 0;
part = size * times + rank + 1;
if (jud(part)) {
num++;
}
}
MPI_Reduce(&num, &number, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (rank == 0) {
if (tot >= 1) {
number++;
}
printf("%d", number);
}
MPI_Finalize();
return 0;
}
标签:int,质数,number,MPI,并行计算,include
From: https://blog.csdn.net/qq_68898943/article/details/139601405