Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn r-squared [x y]
- (+ (* (- 0.5 x) (- 0.5 x))
- (* (- 0.5 y) (- 0.5 y))))
- (defn calculate-pi [t]
- (loop [hits 0
- throws-left t] ; initial number of throws
- (cond ;kinda like if-elseif-else...
- (zero? throws-left)
- (* 4.0 (/ t hits)); all thrown, return value
- (< (r-squared (rand) (rand)) 0.25) ; not bothering with a let here.
- (recur (inc hits) (dec throws-left)) ; hit, inc hits
- :else
- (recur hits (dec throws-left))))) ; no hit, restart, but don't inc hits
- (println (calculate-pi 10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement