Advertisement
utoft

I2DOA - Lab0 - Ex14

Jan 29th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <map>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. #define RUNS 2000
  8. #define PI 3.141592653589793238463
  9.  
  10. struct calculation
  11. {
  12.     int matched;
  13.     int total;
  14. };
  15.  
  16. double makerand()
  17. {
  18.     return (double)rand() / RAND_MAX;
  19. }
  20.  
  21. double getPi(int matched, int total)
  22. {
  23.     return 4*(double)matched / total;
  24. }
  25.  
  26. bool checkGoodPrescision(int matched, int total,int prec)
  27. {
  28.     return floor(getPi(matched,total)*pow(10,prec)) == floor(PI*pow(10,prec));
  29. }
  30.  
  31. int main()
  32. {
  33.     int matched = 0;
  34.     int total = 0;
  35.     //int prec = 1;
  36.     map<int,calculation> prescitions;
  37.  
  38.     for(short prec = 1; prec < 10; ++prec)
  39.     {
  40.         //cout << "Prescition is set to " << prec << " decimals" << endl;
  41.         //cout << "Starting calculations"<< endl;
  42.         while(!checkGoodPrescision(matched,total,prec))
  43.         {
  44.             //Loop a few times before we check
  45.             short toSkip = log(total+1)+1;
  46.             for(int i = 0; i < toSkip;++i)
  47.             {
  48.                 //if(sqrt(2) * sqrt( pow(makerand(),2) ) <=1)
  49.                 if(sqrt( pow(makerand(),2) + pow(makerand(),2) ) <=1)
  50.                     ++matched;
  51.                 ++total;
  52.             }
  53.  
  54.             //cout << "\tCurrent iterations: " << total << " pi: " << getPi(matched,total) << endl;
  55.         }
  56.  
  57.         calculation calc;
  58.         calc.matched = matched;
  59.         calc.total = total;
  60.         prescitions[prec] = calc;
  61.  
  62.         cout << "###### RESULTS ######" << endl;
  63.         for(auto prec = prescitions.begin(); prec != prescitions.end();++prec)
  64.         {
  65.             cout << "Prescition: " << (*prec).first << endl;
  66.             cout << "\tPi was found to be: " << getPi((*prec).second.matched,(*prec).second.total) << endl;
  67.             cout << "\tIt took " << (*prec).second.total << " iterations. " << (*prec).second.matched << " of them matched" << endl;
  68.         }
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74. //RESULTS
  75. Prescition: 1
  76.         Pi was found to be: 3.16667
  77.         It took 24 iterations. 19 of them matched
  78. Prescition: 2
  79.         Pi was found to be: 3.14504
  80.         It took 131 iterations. 103 of them matched
  81. Prescition: 3
  82.         Pi was found to be: 3.1411
  83.         It took 163 iterations. 128 of them matched
  84. Prescition: 4
  85.         Pi was found to be: 3.1415
  86.         It took 329683 iterations. 258925 of them matched
  87. Prescition: 5
  88.         Pi was found to be: 3.1416
  89.         It took 329891 iterations. 259096 of them matched
  90. Prescition: 6
  91.         Pi was found to be: 3.14159
  92.         It took 329904 iterations. 259106 of them matched
  93. Prescition: 7
  94.         Pi was found to be: 3.14159
  95.         It took 353161 iterations. 277372 of them matched
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement