Skip to content

Instantly share code, notes, and snippets.

@jsoffer
Created January 24, 2012 20:07
Show Gist options
  • Save jsoffer/1672258 to your computer and use it in GitHub Desktop.
Save jsoffer/1672258 to your computer and use it in GitHub Desktop.
paradoja del cumpleaños, para 3 personas
import Math.Combinat.Numbers
import Data.Ratio
-- OEIS A141765: el número de cadenas de longitud n en un alfabeto de
-- longitud r que no repiten más de dos veces el mismo símbolo
t :: Integer -> Integer -> Integer
t r n = sum $ map g [a..r] where
a = quot (n+1) 2
g k = if n < k then 0 else
quot (factorial n * binomial r k * binomial k (n-k)) (2^(n-k))
-- Hay que usar 'Ratio' antes que 'fromRational' porque dividir dos
-- valores de punto flotante tan grandes produce error, y por encima
-- de n=120 el cociente resulta NaN
probabilidad :: Integer -> Integer -> Double
probabilidad r n = fromRational (favorables % totales) where
totales, favorables :: Integer
totales = r^n
favorables = totales - t r n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment