Skip to content

Instantly share code, notes, and snippets.

@latant
Last active October 24, 2021 12:59
Show Gist options
  • Save latant/289d67b0a08e6c043b616d28ddae9047 to your computer and use it in GitHub Desktop.
Save latant/289d67b0a08e6c043b616d28ddae9047 to your computer and use it in GitHub Desktop.
Calculation on the possibility that there is two dealt card with the distance of 1 in the card game called 'The MInd'.
(def nnv
(memoize
(fn [n k]
(cond
(= k 0) 1
(> (* 2 k) (+ 1 n)) 0
(= (* 2 k) (+ 1 n)) 1
:else (bigint
(+ (nnv (dec n) k)
(nnv (dec (dec n)) (dec k))))))))
(defn ! [x]
(reduce (comp bigint *)
(range 1 (inc x))))
(defn c [n k]
(/ (! n)
(* (! k)
(! (- n k)))))
(defn p [n k]
(->> (/ (nnv n k)
(c n k))
(double)
(- 1)))
; (p 100 18) => 0.9756332632583158
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment