Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Created July 22, 2012 04:57
Show Gist options
  • Save lambda-fairy/3158496 to your computer and use it in GitHub Desktop.
Save lambda-fairy/3158496 to your computer and use it in GitHub Desktop.
Birthday paradox calculator
-- | Birthday paradox calculator
import Control.Monad
import Data.Ratio
import System.Environment
import Text.Printf
birthday :: Fractional a => Integer -> Integer -> a
birthday days n = realToFrac $ 1 - inverse
where
inverse = product $ zipWith (%) [days, days-1 .. days-n+1] (repeat days)
birthday' :: Fractional a => Integer -> a
birthday' = birthday 365
main :: IO ()
main = do
args <- getArgs
case args of
[n'] -> printf "%.3f\n" (birthday' n :: Double)
where n = read n'
_ -> epicDisplay
epicDisplay :: IO ()
epicDisplay = forM_ [1 .. 50] $ \n ->
printf "%d: %.3f\n" n (birthday' n :: Double)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment