Advertisement
MeehoweCK

Untitled

Nov 19th, 2020
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. double liczba_pi(unsigned long long N)
  8. {
  9.     double x, y;
  10.     unsigned long long n = 0;
  11.  
  12.     srand(time(nullptr));
  13.  
  14.     for(unsigned long long i = 0; i < N; ++i)
  15.     {
  16.         // losujemy punkt:
  17.         x = (rand() - 1) / 32766.0;
  18.         y = (rand() - 1) / 32766.0;
  19.  
  20.         // sprawdzamy czy wylosowany punkt zawiera się w ćwierćkole:
  21.         if(x * x + y * y <= 1)
  22.             ++n;
  23.     }
  24.  
  25.     // zwracamy wynik:
  26.     return 4.0 * n / N;
  27. }
  28.  
  29. int main()
  30. {
  31.     cout << "Liczbe PI oszacowano na " << liczba_pi(10000000000) << endl;
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement