Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
924B

  1. #include "fcpp.hh"
  2. unsigned int x = 93267;
  3. unsigned int zufall ()
  4. {
  5. unsigned int ia = 16807, im = 2147483647;
  6. unsigned int iq = 127773, ir = 2836;
  7. unsigned int k;
  8. k = x/iq; // LCG xneu=(a*xalt) mod m
  9. x = ia*(x-k*iq)-ir*k; // a = 7^5, m = 2^31-1
  10. if (x<0) x = x+im; // keine lange Arithmetik
  11. return x; // s. Numerical Recipes
  12. } // in C, Kap. 7.
  13. unsigned int ggT (unsigned int a, unsigned int b)
  14. {
  15. if (b==0) return a;
  16. else return ggT(b,a%b);
  17. }
  18. int experiment ()
  19. {
  20. unsigned int x1,x2;
  21. x1 = zufall(); x2 = zufall();
  22. if (ggT(x1,x2)==1)
  23. return 1;
  24. else
  25. return 0;
  26. }
  27. double montecarlo (int N)
  28. {
  29. int erfolgreich=0;
  30. for (int i=0; i<N; i=i+1)
  31. erfolgreich = erfolgreich+experiment();
  32. return ((double)erfolgreich)/((double)N);
  33. }
  34. int main (int argc, char** argv) {
  35. print(sqrt(6.0/montecarlo(readarg_int(argc,argv,1))));
  36. }