Skip to content

Instantly share code, notes, and snippets.

@citizen428
Created April 13, 2010 22:14
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 citizen428/365172 to your computer and use it in GitHub Desktop.
Save citizen428/365172 to your computer and use it in GitHub Desktop.
user=> ; get the expt funtion
user=> (use '[clojure.contrib.math :only (expt)])
nil
user=> (expt 2 3)
8
user=> ; function to calculate no. of pairs for n people
user=> (defn number-pairs [n] (/ (* n (- n 1)) 2))
#'user/number-pairs
user=> ; how many pairs for 23 people?
user=> (number-pairs 23)
253
user=> ; the probability for 2 people to have a different birthday is
user=> (/ 364 365)
364/365
user=> ; or as a float
user=> (float (/ 364 365))
0.9972603
user=> ; however, for 23 people this goes down to
user=> (expt (/ 364 365) (number-pairs 23))
909180396911520953237214479303442291548576670983319142126845854379609751298780457275079685102104478094555993199250222212802852305203714676053851442958469375992009968449679316317742908353135596098516403710696591911366727187066295770908503869348698149885249114705137143342653833368599750235956256811176487173612281539486873239189011773784208925485015596963363652012801595139904549317045724517969592522650271677082031913576523744981284680128303709158131176294261138929135637191720157292068719979865018207380801925313657131698883977564930277830448325006712323083121669974985158885925598089505482751270318170236784714282908297691279225474002634663788544/1820097727778608537517729849433925129700612717163497664309510277658180102021641352734338295662966934138513486982003291980909854424103220600226088323712621618998333910131633982535056013882203713733518607272262343204928492982986531163173657962851957557994500618832564906590653632102735909638338530730394773179219648548251019031930311189013378764266385780664874647601642046439264153497936457863395364141642654244768218231806632028198040845101797305246951124040450421282300775977380166343054621871792052337824945935859515547529624017014907162065730420351557283327550381151744473651961187905524283183434496537667253435177627807206590659916400909423828125
user=> ; here's the float again:
user=> (float (expt (/ 364 365) (number-pairs 23)))
0.49952284
user=> ; 49.95% prob. of NO match, subtract from 1 to get prob. FOR match
user=> (float (- 1 (expt (/ 364 365) (number-pairs 23))))
0.50047714
user=> ; tara, over 50% as promised
user=> ; let's wrap this up
user=> (def different-bday-chance (/ 364 365))
#'user/different-bday-chance
user=> (defn shared-birthday-chance
[n]
(- 1 (expt different-bday-chance (number-pairs n))))
#'user/shared-birthday-chance
user=> ; for 57 people, there's almost a 99% chance of a shared birthday
user=> (float (shared-birthday-chance 57))
0.98745716
(ns birthday-paradox
(:use [incanter charts]))
(defn number-pairs
"Returns possible number of pairs for n people"
[n]
(/ (* n (dec n)) 2))
(defn shared-bday-prop
"Probability of shared birthday in a group of n people"
[n]
(- 1 (pow (/ 364 365) (number-pairs n))))
(view (function-plot shared-bday-prop 2 71 :step-size 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment