I cannot find my error in this Scheme program for calculating PI

I am doing an experiment in Monte Carlo to calculate the PI approximation. From SICP:

The Monte Carlo method consists of selecting random experiments from a large set, and then deducing based on probability from tabulating the results of these experiments. For example, we can approximately, using the fact that 6 / pi ^ 2 is the probability that two integers chosen at random will not have common factors; that is, their greatest common factor will be 1. We get an approximation to, we perform a large number of experiments. In each experiment, we randomly select two integers and run a test to see if their GCD is 1. The fraction of the time the test passes gives our estimate of 6 / pi ^ 2 and from this we get our approximation to pi.

But when I run my program, I get values ​​like 3.9 ...

Here is my program:

(define (calculate-pi trials)
  (define (this-time-have-common-factors?)
    (define (get-rand)
      (+ (random 9999999999999999999999999999999) 1))
    (= (gcd (get-rand) (get-rand)) 1))
  (define (execute-experiment n-times acc)
    (if (> n-times 0)
        (if (this-time-have-common-factors?)
            (execute-experiment (- n-times 1) acc)
            (execute-experiment (- n-times 1) (+ acc 1)))
        acc))
  (define n-success (execute-experiment trials 0))
  (define prob (/ n-success trials))
  (sqrt (/ 6 prob)))

My interpreter is MIT / GNU 7.7.90

Thanks for any help.

+3
2

, , if-statement ; .

    (if (this-time-have-common-factors?)
        (execute-experiment (- n-times 1) (+ acc 1)
        (execute-experiment (- n-times 1) acc))

, 1 - 6/& pi; 2 , . "pi" = sqrt (6/(1 - 6/& pi; 2)) = sqrt (6 & pi; 2/(& pi; 2 -6)) = 3.911).


, - (: . ?)...

0 1, p = 6/& pi; 2. .

& rho; = m/n, , . a p p (1-p)/n std dev & sigma; & rho;= sqrt (p (1-p)/n). n = 10000 , std dev 0,00488. 95% , 2- devs , 5% 2- , 0,5982 0,6177. , & pi; , n = 10000, 3.117 3.167 95% 5% .

100, std dev 10 & pi; 3.1391 3.1441 95%.

- , LOTS , .

, pi, .

+11

. . .

:

(define (calculate-pi trials)
  (define (this-time-have-common-factors?)
    (define (get-rand)
      (+ (random 9999999) 1))
    (= (gcd (get-rand) (get-rand)) 1))
  (define (execute-experiment n-times acc)
    (if (> n-times 0)
        (if (this-time-have-common-factors?)
            (execute-experiment (- n-times 1) (+ acc 1))
            (execute-experiment (- n-times 1) acc))
        acc))
  (define n-success (execute-experiment trials 0))
  (define prob (/ n-success trials))
  (sqrt (/ 6 prob)))
+3

Source: https://habr.com/ru/post/1715820/


All Articles