Advertisement
Guest User

Untitled

a guest
Sep 19th, 2010
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.55 KB | None | 0 0
  1. (defn r-squared [x y]
  2.  (+ (* (- 0.5 x) (- 0.5 x))
  3.     (* (- 0.5 y) (- 0.5 y))))
  4.  
  5. (defn calculate-pi [t]
  6.   (loop [hits 0
  7.          throws-left t] ; initial number of throws
  8.     (cond ;kinda like if-elseif-else...
  9.       (zero? throws-left)
  10.         (* 4.0 (/ t hits)); all thrown, return value
  11.       (< (r-squared (rand) (rand)) 0.25) ; not bothering with a let here.
  12.         (recur (inc hits) (dec throws-left)) ; hit, inc hits
  13.       :else
  14.         (recur hits (dec throws-left))))) ; no hit, restart, but don't inc hits  
  15.  
  16. (println (calculate-pi 10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement