Skip to content

Instantly share code, notes, and snippets.

@ekoontz
Created September 5, 2023 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekoontz/6dcc5d793cfcce4182e44079fd6060cb to your computer and use it in GitHub Desktop.
Save ekoontz/6dcc5d793cfcce4182e44079fd6060cb to your computer and use it in GitHub Desktop.
probability of reaching m heads given n coin tosses
(let [n 10
m 5
expt (clojure.math.numeric-tower/expt 2 n)
number-of-heads (->> (range expt)
(map (fn [i] (count (re-seq #"1" (Integer/toBinaryString i)))))
(filter #(or (>= % m)))
count)]
[expt number-of-heads (+ 0.0 (/ number-of-heads expt))])
[1024 638 0.623046875]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment