#include <stdio.h>
#include <stdbool.h>
#include <mpi.h>
#define MAX 100000
// �P�_��ƪ����
bool is_prime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main(int argc, char *argv[]) {
int rank, size;
// ��l��MPI����
MPI_Init(&argc, &argv);
// ������e�i�{��rank�]�s���^�H���`�i�{��
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// �C�Ӷi�{�B�z���d��
int range_per_process = MAX / size;
int start = rank * range_per_process + 2;
int end = (rank + 1) * range_per_process + 1;
if (rank == size - 1) {
// �̫�@�Ӷi�{�B�z�Ѿl���d��
end = MAX;
}
// �C�Ӷi�{��X��B�z�d�����
for (int num = start; num <= end; num++) {
if (is_prime(num)) {
printf("Process %d: %d\n", rank
, num
); }
}
// ���ݩҦ��i�{����
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRib29sLmg+CiNpbmNsdWRlIDxtcGkuaD4KCiNkZWZpbmUgTUFYIDEwMDAwMAoKLy8g77+9UO+/vV/vv73vv73Gqu+/ve+/ve+/ve+/vQpib29sIGlzX3ByaW1lKGludCBudW0pIHsKICAgIGlmIChudW0gPD0gMSkgcmV0dXJuIGZhbHNlOwogICAgZm9yIChpbnQgaSA9IDI7IGkgKiBpIDw9IG51bTsgaSsrKSB7CiAgICAgICAgaWYgKG51bSAlIGkgPT0gMCkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHRydWU7Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIHsKICAgIGludCByYW5rLCBzaXplOwogICAgCiAgICAvLyDvv73vv71s77+977+9TVBJ77+977+977+977+9CiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOwogICAgCiAgICAvLyDvv73vv73vv73vv73vv73vv71l77+9ae+/vXvvv73vv71yYW5r77+9Xe+/vXPvv73vv73vv71e77+9SO+/ve+/ve+/vWDvv71p77+9e++/ve+/vQogICAgTVBJX0NvbW1fcmFuayhNUElfQ09NTV9XT1JMRCwgJnJhbmspOwogICAgTVBJX0NvbW1fc2l6ZShNUElfQ09NTV9XT1JMRCwgJnNpemUpOwoKICAgIC8vIO+/vUPvv73Ttmnvv71777+9Qu+/vXrvv73vv73vv71k77+977+9CiAgICBpbnQgcmFuZ2VfcGVyX3Byb2Nlc3MgPSBNQVggLyBzaXplOwogICAgaW50IHN0YXJ0ID0gcmFuayAqIHJhbmdlX3Blcl9wcm9jZXNzICsgMjsKICAgIGludCBlbmQgPSAocmFuayArIDEpICogcmFuZ2VfcGVyX3Byb2Nlc3MgKyAxOwoKICAgIGlmIChyYW5rID09IHNpemUgLSAxKSB7CiAgICAgICAgLy8g77+9zKvvv71A77+907Zp77+9e++/vULvv71677+90b5s77+977+977+9ZO+/ve+/vQogICAgICAgIGVuZCA9IE1BWDsKICAgIH0KCiAgICAvLyDvv71D77+907Zp77+9e++/ve+/vVjvv73vv71C77+9eu+/vWTvv73ypLqq77+977+977+977+9CiAgICBmb3IgKGludCBudW0gPSBzdGFydDsgbnVtIDw9IGVuZDsgbnVtKyspIHsKICAgICAgICBpZiAoaXNfcHJpbWUobnVtKSkgewogICAgICAgICAgICBwcmludGYoIlByb2Nlc3MgJWQ6ICVkXG4iLCByYW5rLCBudW0pOwogICAgICAgIH0KICAgIH0KCiAgICAvLyDvv73vv73vv73dqdKm77+977+9ae+/vXvvv73vv73vv73vv70KICAgIE1QSV9GaW5hbGl6ZSgpOwogICAgCiAgICByZXR1cm4gMDsKfQoK