#include <stdio.h>
#include <omp.h>
int main() {
long long n = 100000000; // Number of terms
double pi = 0.0;
// Start the parallel region
#pragma omp parallel
{
double sum = 0.0;
// Use a private variable for each thread
#pragma omp for
for (long long k = 0; k < n; k++) {
// Calculate the term and accumulate in the private sum
sum += (k % 2 == 0 ? 1.0 : -1.0) / (2.0 * k + 1.0);
}
// Combine results from all threads
#pragma omp atomic
pi += sum;
}
pi *= 4; // Multiply by 4 to get the final value of π
printf("Approximation of PI: %.15f\n", pi);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxvbXAuaD4KCmludCBtYWluKCkgewogICAgbG9uZyBsb25nIG4gPSAxMDAwMDAwMDA7IC8vIE51bWJlciBvZiB0ZXJtcwogICAgZG91YmxlIHBpID0gMC4wOwoKICAgIC8vIFN0YXJ0IHRoZSBwYXJhbGxlbCByZWdpb24KICAgICNwcmFnbWEgb21wIHBhcmFsbGVsCiAgICB7CiAgICAgICAgZG91YmxlIHN1bSA9IDAuMDsKICAgICAgICAvLyBVc2UgYSBwcml2YXRlIHZhcmlhYmxlIGZvciBlYWNoIHRocmVhZAogICAgICAgICNwcmFnbWEgb21wIGZvcgogICAgICAgIGZvciAobG9uZyBsb25nIGsgPSAwOyBrIDwgbjsgaysrKSB7CiAgICAgICAgICAgIC8vIENhbGN1bGF0ZSB0aGUgdGVybSBhbmQgYWNjdW11bGF0ZSBpbiB0aGUgcHJpdmF0ZSBzdW0KICAgICAgICAgICAgc3VtICs9IChrICUgMiA9PSAwID8gMS4wIDogLTEuMCkgLyAoMi4wICogayArIDEuMCk7CiAgICAgICAgfQoKICAgICAgICAvLyBDb21iaW5lIHJlc3VsdHMgZnJvbSBhbGwgdGhyZWFkcwogICAgICAgICNwcmFnbWEgb21wIGF0b21pYwogICAgICAgIHBpICs9IHN1bTsKICAgIH0KCiAgICBwaSAqPSA0OyAvLyBNdWx0aXBseSBieSA0IHRvIGdldCB0aGUgZmluYWwgdmFsdWUgb2Ygz4AKCiAgICBwcmludGYoIkFwcHJveGltYXRpb24gb2YgUEk6ICUuMTVmXG4iLCBwaSk7CiAgICByZXR1cm4gMDsKfQ==