fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. int main() {
  5. long long n = 100000000; // Number of terms
  6. double pi = 0.0;
  7.  
  8. // Start the parallel region
  9. #pragma omp parallel
  10. {
  11. double sum = 0.0;
  12. // Use a private variable for each thread
  13. #pragma omp for
  14. for (long long k = 0; k < n; k++) {
  15. // Calculate the term and accumulate in the private sum
  16. sum += (k % 2 == 0 ? 1.0 : -1.0) / (2.0 * k + 1.0);
  17. }
  18.  
  19. // Combine results from all threads
  20. #pragma omp atomic
  21. pi += sum;
  22. }
  23.  
  24. pi *= 4; // Multiply by 4 to get the final value of π
  25.  
  26. printf("Approximation of PI: %.15f\n", pi);
  27. return 0;
  28. }
Success #stdin #stdout 0.38s 5284KB
stdin
Standard input is empty
stdout
Approximation of PI: 3.141592643589326