Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def throws 10)
- (defn r-squared [x y]
- (+ (* (- 0.5 x) (- 0.5 x))
- (* (- 0.5 y) (- 0.5 y))))
- (loop [hits 0]
- (let [x (rand)
- y (rand)]
- ; still inside the let
- (if (< (r-squared x y) 0.25)
- (recur (inc hits)) ; then expression, restarts the loop
- (* 4 (/ hits throws))))) ; else-expression, the return value of the loop
- ; compare
- (defn calculate-pi [hits]
- (let [x (rand)
- y (rand)]
- ; still inside the let
- (if (< (r-squared x y) 0.25)
- (recur (inc hits)) ; then expression, restarts the function
- (* 4 (/ hits throws))))) ; else-expression, the return value of the function
- (println (calculate-pi 0)) ; probably not working :P
Advertisement
Add Comment
Please, Sign In to add comment