Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Last active January 4, 2025 21:04
Show Gist options
  • Save evgenii-malov/4ebfdf1173efc5ed8e686e7eb67ae46e to your computer and use it in GitHub Desktop.
Save evgenii-malov/4ebfdf1173efc5ed8e686e7eb67ae46e to your computer and use it in GitHub Desktop.
{-
binomial-distribution-1: https://www.hackerrank.com/challenges/binomial-distribution-1/
Author: Evgeniy Malov <[email protected]>
Date: Jan 4, 2025
join me : https://www.youtube.com/@EvgeniyMalov
https://ru.linkedin.com/in/deepwalk
-}
import Text.Printf (printf)
-- 4 shots
-- more than 2 hits - it is equal to
-- A - event of 3 or 4 hits
-- P(A) = ?
-- B = 1 miss
-- P(A) = 1 - P(B) = ?
-- at least 3 misses
-- A - 3 or 4 misses
-- B - 1 hit or 0 hits
-- P(A) = 1 - P(B)
-- P(K) = C(N,K)*P^K*Q^(N-K)
fac 0 = 1
fac n = n*(fac $ n - 1)
cnk k n = (fac n) `div` ( (fac (n - k)) * fac (k))
binp n k p = (fromIntegral (cnk k n)) * (p ^ k) * ((1 - p)^(n-k))
p :: Double
p = 4 / 5
p1 = binp 4 3 p + binp 4 4 p
p2 = binp 4 1 p + binp 4 0 p
main = do
printf "%.3f\n" p1
printf "%.3f" p2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment