MeehoweCK

Untitled

Nov 19th, 2020
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. #define DIV (rand() - 1) / 32766.0      // defining a macro - everytime when we put DIV, it's replaced by this formula (rand() - 1) / 32766.0
  6.  
  7. using namespace std;
  8.  
  9. double PI_number(unsigned long long N)
  10. {
  11.     double x, y;
  12.     unsigned long long n = 0;       // counts the number of the points included of the circle
  13.     srand(time(nullptr));
  14.     for(unsigned i = 0; i < N; ++i)
  15.     {
  16.         x = DIV;
  17.         y = DIV;
  18.         if(x * x + y * y <= 1)
  19.             ++n;
  20.     }
  21.     return 4.0 * n / N;
  22. }
  23.  
  24. int main()
  25. {
  26.     cout << "PI has been estimated at " << PI_number(10000000000) << endl;
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment