Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <chrono>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. int i;
  12. int j=0;
  13. int N=1;
  14. double x;
  15. double z;
  16. double err;
  17.  
  18. auto start = std::chrono::system_clock::now();
  19. do
  20. {
  21. x=0;
  22.  
  23. for(i=1; i<=N; i++) // wartosc PI
  24. {
  25. z=pow((i-0.5)/N,2);
  26. x =x+ 1/(1+z);
  27. }
  28. x=4*x/N;
  29.  
  30. //cout<<x<<endl;
  31.  
  32. err = x-M_PI;
  33. N=N+10;
  34.  
  35. }while(err>=1.5e-6);
  36.  
  37. do
  38. {
  39. x=0;
  40.  
  41. for(i=1; i<=N; i++) // PI value
  42. {
  43. z=pow((i-0.5)/N,2);
  44. x =x+ 1/(1+z);
  45. }
  46. x=4*x/N;
  47.  
  48. //cout<<x<<endl;
  49.  
  50. err = x-M_PI;
  51. N++;
  52.  
  53. }while(err>=1e-6);
  54. auto end = std::chrono::system_clock::now();
  55.  
  56. std::chrono::duration<double> elapsed_seconds = end-start;
  57. std::time_t end_time = std::chrono::system_clock::to_time_t(end);
  58.  
  59. cout<<"Nmin is = "<<N-1<<endl<<"Error for Nmin is = "<<err<<endl<<endl;
  60.  
  61. std::cout << "finished computation at " << std::ctime(&end_time)
  62. << "elapsed time: " << elapsed_seconds.count() << "s\n";
  63.  
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement