PasteRack.org
Paste # 6450
2016-10-21 01:16:54

Fork as a new paste.

Paste viewed 72 times.


Embed:

  1.  
  2. (define (estimate-pi trials)
  3.         (define (cesaro-test)
  4.                 (= (gcd (random 4294967087) (random 4294967087)) 1))
  5.         (sqrt (/ 6 (monte-carlo trials cesaro-test))))
  6. (define (monte-carlo trials experiment)
  7.         (define (iter trials-remaining trials-passed)
  8.                 (cond ((= trials-remaining 0)
  9.                                         (/ trials-passed trials))
  10.                                         ((experiment)
  11.                                                 (iter (- trials-remaining 1)
  12.                                                                         (+ trials-passed 1)))
  13.                                         (else
  14.                                                 (iter (- trials-remaining 1)
  15.                                                                         trials-passed))))
  16. (iter trials 0))
  17. ;------------------------------------------
  18. ;exercise 3-5
  19. (define (estimate-integral P x1 x2 y1 y2 trials)
  20.         (define (experiment)
  21.                         (P (random x1 (+ x2 1))
  22.                                 (random y1 (+ y2 1))))
  23.         ; (* area (monte-carlo trials experiment)) area * portion
  24.         (exact->inexact (* (* (abs (- x2 x1)) (abs (- y2 y1)))
  25.                 (monte-carlo trials experiment))))
  26.  
  27. ; the region is described by P(x, y) that is true for p(x,y) in the region and false otherwise.
  28. ; problem here
  29. (define (make-circle-predicate a b r)
  30.         ;make a predicate such as (x-a)^2 + (y-b)^2 <= r^2
  31.         ; return lambda (x y) according to above formula
  32.         (lambda (x y)
  33.                 (<= (+ (sqr (- x a)) (sqr (- y b)))
  34.                                 (sqr r))))
  35.  
  36. (define c1 (make-circle-predicate 4 4 1))
  37. (estimate-integral c1 3 5 3 5 100000)

=>

load-handler: expected a `module' declaration for `pasterack' in "paste 6450", but found something else

Check that paste includes #lang?