Advertisement
Nikmosi

ak pz1 ex p1.2 with alg

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