Advertisement
Nikmosi

ak pz1 ex p1.2 without alg

Apr 3rd, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <cstdio>
  2. #include <chrono>
  3.  
  4. using namespace  std::chrono;
  5.  
  6. double pi(int N)
  7. {
  8.    srand(time(nullptr));
  9.    int M = 0;
  10.    double x = -1, y = -1;
  11.    for (int i = 0; i < N; i++)
  12.    {
  13.       x = (rand() / 32767.0 * 2) - 1;
  14.       y = (rand() / 32767.0 * 2) - 1;
  15.       if (std::pow(x, 2) + std::pow(y, 2) <= 1)
  16.          M++;
  17.    }
  18.  
  19.    return M * 4.0 / N;
  20. }
  21.  
  22. int main()
  23. {
  24.    double N = 0;
  25.    printf_s("count: ");
  26.    scanf_s("%lf", &N);
  27.  
  28.    auto s = high_resolution_clock::now();
  29.    auto p = pi(N);
  30.    auto t = duration_cast<duration<double, std::ratio<1>>> (high_resolution_clock::now() - s).count();
  31.  
  32.    printf_s("pi: %lf; time: %lf s\n", p, t);
  33.    return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement