/* Three players coin toss in Picat. From https://x.com/Riazi_Cafe_en/status/1978749212150878208 """ Three players take turns tossing a coin. If a player gets heads, they are eliminated; if tails, they remain in the game. On average, how many coin tosses occur before all players are eliminated? """ Picat> X=pascal_dist_mean(3,1/2) X = 6.0 Histogram (random generated) Picat> pascal_dist_n(3,1/2,10000).show_histogram Histogram: 3: 1216 ####################################### (0.122 / 0.122) 4: 1836 ########################################################## (0.184 / 0.305) 5: 1884 ############################################################ (0.188 / 0.494) 6: 1590 ################################################### (0.159 / 0.653) 7: 1179 ###################################### (0.118 / 0.771) 8: 821 ########################## (0.082 / 0.853) 9: 526 ################# (0.053 / 0.905) 10: 378 ############ (0.038 / 0.943) 11: 232 ####### (0.023 / 0.966) 12: 129 #### (0.013 / 0.979) 13: 92 ### (0.009 / 0.988) 14: 48 ## (0.005 / 0.993) 15: 37 # (0.004 / 0.997) 16: 19 # (0.002 / 0.999) 17: 7 (0.001 / 0.999) 18: 4 (0.000 / 1.000) 19: 2 (0.000 / 1.000) 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 : tosses Probabilities: 4: 0.1896000000000000 5: 0.1867000000000000 6: 0.1541000000000000 3: 0.1230000000000000 7: 0.1208000000000000 8: 0.0845000000000000 9: 0.0539000000000000 10: 0.0318000000000000 11: 0.0238000000000000 12: 0.0129000000000000 13: 0.0067000000000000 14: 0.0055000000000000 15: 0.0023000000000000 16: 0.0018000000000000 17: 0.0016000000000000 19: 0.0003000000000000 18: 0.0003000000000000 20: 0.0002000000000000 23: 0.0001000000000000 21: 0.0001000000000000 mean = 6.0009 */ go ?=> reset_store, run_model(10_000,$model,[show_probs,mean]), nl, % show_store_lengths, % fail, nl. go => true. model() => N = 3, Tosses = 0, while (N > 0) Tosses := Tosses + 1, if flip(1/2) == true then N := N -1 end end, add("tosses",Tosses).