Facebook
From Sweet Guinea Pig, 6 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 249
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. long long num_steps = 1000000000;
  5. double step;
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.         clock_t start, stop;
  10.         double x, pi, sum=0.0;
  11.         int i;
  12.         step = 1./(double)num_steps;
  13.         start = clock();
  14.         for (i=0; i<num_steps; i++)
  15.         {
  16.                 x = (i + .5)*step;
  17.                 sum = sum + 4.0/(1.+ x*x);
  18.         }
  19.        
  20.         pi = sum*step;
  21.         stop = clock();
  22.  
  23.         printf("Wartosc liczby PI wynosi %15.12f\n",pi);
  24.         printf("Czas przetwarzania wynosi %f sekund\n",((double)(stop - start)/1000.0));
  25.         return 0;
  26. }