fork download
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <mpi.h>
  4.  
  5. #define MAX 100000
  6.  
  7. // �P�_��ƪ����
  8. bool is_prime(int num) {
  9. if (num <= 1) return false;
  10. for (int i = 2; i * i <= num; i++) {
  11. if (num % i == 0) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17.  
  18. int main(int argc, char *argv[]) {
  19. int rank, size;
  20.  
  21. // ��l��MPI����
  22. MPI_Init(&argc, &argv);
  23.  
  24. // ������e�i�{��rank�]�s���^�H���`�i�{��
  25. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  26. MPI_Comm_size(MPI_COMM_WORLD, &size);
  27.  
  28. // �C�Ӷi�{�B�z���d��
  29. int range_per_process = MAX / size;
  30. int start = rank * range_per_process + 2;
  31. int end = (rank + 1) * range_per_process + 1;
  32.  
  33. if (rank == size - 1) {
  34. // �̫�@�Ӷi�{�B�z�Ѿl���d��
  35. end = MAX;
  36. }
  37.  
  38. // �C�Ӷi�{��X��B�z�d�򤺪����
  39. for (int num = start; num <= end; num++) {
  40. if (is_prime(num)) {
  41. printf("Process %d: %d\n", rank, num);
  42. }
  43. }
  44.  
  45. // ���ݩҦ��i�{����
  46. MPI_Finalize();
  47.  
  48. return 0;
  49. }
  50.  
  51.  
Success #stdin #stdout #stderr 0.29s 40856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted