/* Picking 3 of the same color in Picat. From https://www.reddit.com/r/askmath/comments/1fani5o/a_maths_problem_my_dad_has_to_do_for_work_and/ """ A maths problem my dad has to do for work and nobody can figure it out Probability If I have a bag with 6 balls (3 blue and 3 red) and I pick out all 6, at which point have I picked 3 of the same colour? this is confusing us because it’s not a normal probability question. we’ve all been able to do “how likely am i to pick out three of the same?”, but we can’t figure out how to do “how many balls do i have to pick until i’ve got 3 of the same?” we’re very confused please help """ Cf my Gamble model gamble_picking_3_of_the_same_color.rkt This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import ppl_distributions, ppl_utils. import util. main => go. /* var : ret Probabilities: 5: 0.6047000000000000 4: 0.3001000000000000 3: 0.0952000000000000 mean = 4.5095 From this we can draw 2 conclusions: 1) The mean number of draws needed is 4.5 ("probability" approach) And I would argue that this is the answer to the asked question. 2) The max number of draws needed is 5 ("logical" approach) Reasoning: - We need at least 3 draws, e.g. [C1,C1,C1] - And we might need 4 draws, e.g. [C1,C2,C1,C1] - But after 5 draws there must be at least one color that has been drawn 3 times (and the other 2 times). E.g. [C1,C2,C1,C2,C1] */ go ?=> reset_store, run_model(10_000,$model,[show_probs_trunc,mean]), nl, % show_store_lengths, % fail, nl. go => true. f(A,NumBlue,NumRed,Total) = Res => Len = A.len, Pick = A.first, A2 = A.tail, Total1 = Total + 1, if NumBlue == 3 ; NumRed == 3 ; Len == 0 then Res = Total else if Pick == blue then Res = f(A2,NumBlue+1,NumRed,Total1) else Res = f(A2,NumBlue,NumRed+1,Total1) end end. model() => Balls = [blue,blue,blue,red,red,red], NumBalls = Balls.len, % Permute the list A = draw_without_replacement(NumBalls,Balls), Ret = f(A,0,0,0), % add("a,ret",[A,Ret]), add("ret",Ret). /* % Another approach I = 0, OK = false, NumRed = 0, NumBlue = 0, while (OK == false) if NumRed == 3 ; NumBlue == 3 then OK := I else I := I + 1, Pick = A[I], if Pick == blue then NumBlue := NumBlue + 1 else NumRed := NumRed + 1 end, end end, add("count",I). */