Advertisement
rajeshinternshala

Untitled

Oct 16th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <omp.h>
  3. #include <stdbool.h>
  4. #include <math.h>
  5.  
  6. void sieveOfEratosthenes(int n) {
  7.     bool prime[n+1];
  8.     #pragma omp parallel for
  9.     for (int i = 0; i <= n; i++) {
  10.         prime[i] = true;
  11.     }
  12.  
  13.     int sqrt_n = (int)sqrt(n);
  14.     #pragma omp parallel for
  15.     for (int p = 2; p <= sqrt_n; p++) {
  16.         if (prime[p]) {
  17.             for (int i = p * p; i <= n; i += p) {
  18.                 prime[i] = false;
  19.             }
  20.         }
  21.     }
  22.  
  23.     #pragma omp parallel for
  24.     for (int p = 2; p <= n; p++) {
  25.         if (prime[p]) {
  26.             #pragma omp critical
  27.             printf("%d ", p);
  28.         }
  29.     }
  30. }
  31.  
  32. int main() {
  33.     int n = 10000;
  34.     printf("Prime numbers up to %d are: ", n);
  35.     sieveOfEratosthenes(n);
  36.     printf("\n");
  37.    
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement