fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. long double f(long double x) {
  8. return sqrt(1.0L - x * x);
  9. }
  10.  
  11. int main() {
  12. const int n = 1000000; // więcej podziałów = większa dokładność
  13. long double a = -1.0L;
  14. long double b = 1.0L;
  15. long double h = (b - a) / n;
  16.  
  17. long double suma = 0.5L * (f(a) + f(b));
  18.  
  19. for(int i = 1; i < n; i++) {
  20. long double x = a + i * h;
  21. suma += f(x);
  22. }
  23.  
  24. long double pi = 2.0L * h * suma;
  25.  
  26. cout << fixed << setprecision(20);
  27. cout << "Pi (metoda trapezow): " << pi << endl;
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Pi (metoda trapezow): 3.14159265026361382328