-
Notifications
You must be signed in to change notification settings - Fork 2
/
96-problemi-dadi.lsp
4977 lines (4351 loc) · 120 KB
/
96-problemi-dadi.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
===================
PROBLEMI SUI DADI
===================
A Collection of Dice Problems
with solutions and useful appendices
(a work continually in progress)
version July 3, 2022 (76 problems)
Matthew M. Conroy
doctormatt "at" madandmoonly dot com
https://www.madandmoonly.com/doctormatt/mathematics/dice1.pdf
http://www.matthewconroy.com/
Conroy solves the problems mathematically (very informative and interesting).
Here, I try solve the problems (and/or verify the results) with simulations (if I am able...).
========================
Problem Solving Status
========================
+ = solved/verified
- = unsolved
1. Standard Dice (1..26)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
+ + + + + + + + + + + + + + + + + + + + + + + + + +
2. Dice Sums (27..45)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
+ + + + + + + + + + + + + + + + + + +
3. Non-standard Dice (46..55)
46 47 48 49 50 51 52 53 54 55
+ + + + + + + + + +
4. Game with Dice (56..76)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
+ + + + + + + + + + + + + + + + + + + + +
Note: Number of iterations (minimum) = 1e6 (if possible)
Note: The function "time" return the time elapsed in milliseconds.
===================
General Functions
===================
; Check if a positive integer number is prime. Return true or nil.
(define (prime? num) (if (< num 2) nil (= 1 (length (factor num)))))
; Roll a die with S sides
(define (die s) (+ (rand s) 1))
; Roll a die with 6 sides
(define (die6) (+ (rand 6) 1))
; Roll N die with S sides and return the sum of numbers
(define (dice n s) (+ n (apply + (rand s n))))
(dice 10 4)
;-> 19
; Roll N die with 6 sides and return the sum of numbers
(define (dice6 n) (+ n (apply + (rand 6 n))))
(dice6 10)
;-> 38
; Roll N die with S sides and return the list of numbers
(define (dice-lst n s) (map (curry + 1) (rand s n)))
(dice-lst 4 8)
;-> (7 7 5 3)
; Roll N die with 6 sides and return the list of numbers
(define (dice6-lst n) (map (curry + 1) (rand 6 n)))
(dice6-lst 5)
;-> (1 3 2 6 2)
; Roll N die with s sides and return a list with the frequency of all faces
(define (dice-lst-freq n s) (count (sequence 1 s) (dice-lst n s)))
(dice-lst-freq 20 8)
;-> (3 3 3 4 0 5 1 1)
; Roll N die with 6 sides and return a list with the frequency of all faces
(define (dice6-lst-freq n) (count '(1 2 3 4 5 6) (dice6-lst n)))
(dice6-lst-freq 12)
;-> (1 3 0 4 3 1)
; Convert probability (0..1) to "1 in x events"
(define (one-in prob) (int (add 0.5 (div prob))))
(one-in 0.5)
;-> 2
; Roll a non-fair dice with N sides
; Get a list of N probabilities (one prob for each sides)
; and return a random number (0..N-1) with the distribution
; of the probabilities predefined
; Nota: the sum of probabilities must be 1.
(define (rand-pick lst)
(local (rnd stop out)
; generiamo un numero random diverso da 1
; (per evitare errori di arrotondamento)
(while (= (setq rnd (random)) 1))
(setq stop nil)
(dolist (p lst stop)
; sottraiamo la probabilità corrente al numero random...
(setq rnd (sub rnd p))
; se il risultato è minore di zero,
; allora restituiamo l'indice della probabilità corrente
(if (< rnd 0)
(set 'out $idx 'stop true)
)
)
out))
Tests:
(setq dice4-prob '(0.05 0.15 0.35 0.45))
(apply add dice4-prob)
;-> 1
(rand-pick dice4-prob)
;-> 2
(setq dice4-prob '(0.05 0.15 0.35 0.45))
(apply add dice4-prob)
;-> 1
(setq vet (array 4 '(0)))
;-> (0 0 0 0)
(for (i 0 999999) (++ (vet (rand-pick dice4-prob))))
vet
;-> (50087 150175 349614 450124)
(apply + vet)
;-> 1000000
(setq dice6-prob '(0.01 0.01 0.01 0.77 0.1 0.1))
(apply add dice6-prob)
;-> 1
(setq vet (array 6 '(0)))
;-> (0 0 0 0 0 0)
(for (i 0 999999) (++ (vet (rand-pick dice6-prob))))
vet
;-> (9821 10070 9923 770122 100288 99776)
(setq dice6-prob '(0.1 0.0 0.2 0.3 0.0 0.4))
(apply add dice6-prob)
;-> 1
(setq vet (array 6 '(0)))
;-> (0 0 0 0 0 0)
(for (i 0 999999) (++ (vet (rand-pick dice6-prob))))
vet
;-> (100015 0 200371 300148 0 399466)
; Roll a die with N sides with non-standard numeration
; Get a list with the values of each face
; Return an element of the list
(define (rand-lst lst) (lst (rand (length lst))))
(setq d1 '(1 2 2 3 3 4))
(setq d2 '(1 3 4 5 6 8))
(rand-lst d1)
;-> 3
(rand-lst d2)
;-> 6
Probability for d1:
(setq freq (array 5 '(0)))
(for (i 1 1e6) (++ (freq (rand-lst d1))))
(dolist (f freq) (println $idx { } (div f 1e6)))
;-> 0 0
;-> 1 0.166152
;-> 2 0.334015
;-> 3 0.332892
;-> 4 0.166941
Probability for d2:
(setq freq (array 9 '(0)))
(for (i 1 1e6) (++ (freq (rand-lst d2))))
(dolist (f freq) (println $idx { } (div f 1e6)))
;-> 0 0
;-> 1 0.166818
;-> 2 0
;-> 3 0.166644
;-> 4 0.167115
;-> 5 0.166344
;-> 6 0.166272
;-> 7 0
;-> 8 0.166807
;; Module: dice.lsp
;; Dice Functions
(define (die s) (+ (rand s) 1))
(define (die6) (+ (rand 6) 1))
(define (dice n s) (+ n (apply + (rand s n))))
(define (dice6 n) (+ n (apply + (rand 6 n))))
(define (dice-lst n s) (map (curry + 1) (rand s n)))
(define (dice6-lst n) (map (curry + 1) (rand 6 n)))
(define (dice-lst-freq n s) (count (sequence 1 s) (dice-lst n s)))
(define (dice6-lst-freq n) (count '(1 2 3 4 5 6) (dice6-lst n)))
(define (one-in prob) (int (add 0.5 (div prob))))
(define (rand-pick lst)
(local (rnd stop out)
(while (= (setq rnd (random)) 1))
(setq stop nil)
(dolist (p lst stop)
(setq rnd (sub rnd p))
(if (< rnd 0) (set 'out $idx 'stop true))
)
out))
(define (rand-lst lst) (lst (rand (length lst))))
(define (prime? num) (if (< num 2) nil (= 1 (length (factor num)))))
;; eof
==========================
1. Standard Dice (1..26)
==========================
---------
Problem 1
---------
On average, how many times must a 6-sided die be rolled until a 6 turns up?
Solution = 6
(define (p1 iter)
(local (sum trial)
(setq sum 0)
(for (i 1 iter)
(setq trial 1)
(until (= (die6) 6)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p1 1e6)))
;-> 6.008071
;-> 761.984
---------
Problem 2
---------
On average, how many times must a 6-sided die be rolled until a 6 turns up twice in a row?
Solution = 42
(define (p2 iter)
(local (sum trial prev pair val)
(setq sum 0)
(for (i 1 iter)
(setq trial 0)
(setq prev nil)
(setq pair nil)
(until pair
(setq val (die6))
(cond ((and (= val 6) prev) (setq pair true))
((= val 6) (setq prev true))
(true (setq prev nil))
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p2 1e6)))
;-> 41.991021
;-> 8495.761
---------
Problem 3
---------
On average, how many times must a 6-sided die be rolled until the sequence 65 appears (i.e., a 6 followed by a 5)?
Solution = 36
(define (p3 iter)
(local (sum trial prev pair val)
(setq sum 0)
(for (i 1 iter)
(setq trial 0)
(setq prev nil)
(setq pair nil)
(until pair
(setq val (die6))
(cond ((and (= val 5) prev) (setq pair true))
((= val 6) (setq prev true))
(true (setq prev nil))
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p3 1e6)))
;-> 36.000682
;-> 7318.514
---------
Problem 4
---------
On average, how many times must a 6-sided die be rolled until there are two rolls in a row that differ by 1 (such as a 2 followed by a 1 or 3, or a 6 followed by a 5)?
What if we roll until there are two rolls in a row that differ by no more than 1 (so we stop at a repeated roll, too)?
Solution 1 = 4.68627450980
Solution 2 = 3.278260869565
(define (p4-1 iter)
(local (sum trial prev differ1 val)
(setq sum 0)
(for (i 1 iter)
(setq trial 0)
(setq prev -99)
(setq differ1 nil)
(until differ1
(setq val (die6))
(cond ((= (abs (- val prev)) 1) (setq differ1 true))
(true (setq prev val))
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p4-1 1e6)))
;-> 4.68883
;-> 1093.942
(define (p4-2 iter)
(local (sum trial prev differ1 val)
(setq sum 0)
(for (i 1 iter)
(setq trial 0)
(setq prev -99)
(setq differ1 nil)
(until differ1
(setq val (die6))
(cond ((<= (abs (- val prev)) 1) (setq differ1 true))
(true (setq prev val))
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p4-2 1e6)))
;-> 3.27747
;-> 803.129
---------
Problem 5
---------
We roll a 6-sided die n times. What is the probability that all faces have appeared?
Solution =
n Exact values Approximate values
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 5/324 0.01543210
7 35/648 0.05401235
8 665/5832 0.11402606
9 245/1296 0.18904321
10 38045/139968 0.27181213
11 99715/279936 0.35620642
12 1654565/3779136 0.43781568
13 485485/944784 0.51385819
14 317181865/544195584 0.58284535
15 233718485/362797056 0.64421274
16 2279105465/3265173504 0.69800440
17 4862708305/6530347008 0.74463245
18 553436255195/705277476864 0.78470712
19 1155136002965/1410554953728 0.81892308
20 2691299309615/3173748645888 0.84798754
36 0.99154188
(define (p5 n iter)
(local (sum faces)
(setq sum 0)
(for (i 1 iter)
(setq faces (dup 0 7))
(setf (faces 0) -1)
(for (j 1 n)
(++ (faces (die6)))
)
(if (not (ref 0 faces)) (++ sum))
)
(div sum iter)))
(time (println (p5 20 1e6)))
;-> 0.847704
;-> 3000.267
(define (test-p5 n)
(for (i 1 n) (println i { } (p5 i 1e6))))
(time (test-p5 36))
;-> 1 0
;-> 2 0
;-> 3 0
;-> 4 0
;-> 5 0
;-> 6 0.015385
;-> 7 0.053868
;-> 8 0.113604
;-> 9 0.189396
;-> 10 0.271885
;-> 11 0.356252
;-> 12 0.438
;-> 13 0.513837
;-> 14 0.582553
;-> 15 0.644249
;-> 16 0.698467
;-> 17 0.744921
;-> 18 0.7846
;-> 19 0.819185
;-> 20 0.848105
;-> 21 0.872557
;-> 22 0.892951
;-> 23 0.910738
;-> 24 0.924831
;-> 25 0.937707
;-> 26 0.947833
;-> 27 0.956693
;-> 28 0.963554
;-> 29 0.969738
;-> 30 0.974813
;-> 31 0.979082
;-> 32 0.982314
;-> 33 0.985501
;-> 34 0.987878
;-> 35 0.989893
;-> 36 0.991443
;-> 100587.521 ; 100 seconds
---------
Problem 6
---------
We roll a 6-sided die n times. What is the probability that all faces have appeared in order, in some six consecutive rolls (i.e., what is the probability that the subsequence 123456 appears among the n rolls)?
Solution =
n prob
6 0.00002143347051
10 0.0001071673525
20 0.0003214813850
50 0.0009641479102
100 0.002034340799
200 0.004171288550
500 0.01055471585
1000 0.02110296021
2000 0.04186329068
5000 0.1015397384
10000 0.1928556782
(define (p6 n iter)
(local (sum)
(setq sum 0)
(for (i 1 iter)
(if (match '(* 1 2 3 4 5 6 *) (dice6-lst n))
(++ sum)
)
)
(div sum iter)))
(time (println (p6 6 1e6)))
;-> 0.00002700
;-> 858.821
(time (println (p6 10 1e6)))
;-> 0.000107
;-> 1244.377
(time (println (p6 20 1e6)))
;-> 0.000342
;-> 2199.397
(time (println (p6 50 1e6)))
;-> 0.000984
;-> 5087.946
(time (println (p6 100 1e6)))
;-> 0.002134
;-> 9805.423000000001
(time (println (p6 200 1e6)))
;-> 0.004151
;-> 19308.951
(time (println (p6 500 1e6)))
;-> 0.010528
;-> 47747.793
(time (println (p6 1000 1e6)))
;-> 0.020992
;-> 96440.39
(time (println (p6 2000 1e6)))
;-> 0.041803
;-> 194294.68
(time (println (p6 5000 1e5)))
;-> 0.10071
;-> 49892.692
(time (println (p6 10000 1e5)))
;-> 0.19275
;-> 104299.682
For (n > 1000) the following expression is a quite good approximation:
1
prob ≈ 1 - (1 - -----)^n
6^6
---------
Problem 7
---------
We roll a 6-sided die n times.
What is the probability that all faces have appeared in some order in some six consecutive rolls?
What is the expected number of rolls until such a sequence appears?
Solution =
The probability that we have had a run of six distinct faces in six consecutive rolls after rolling n times:
n prob(n)
6 0.015432098765432
7 0.028292181069958
8 0.040723593964334
9 0.052940672153635
10 0.065002953055936
11 0.076924328735457
12 0.088693222472014
13 0.100311784227929
14 0.111782118026154
15 0.123106206915980
16 0.134285932624874
17 0.145323126219390
18 0.156219603871238
19 0.166977159634352
20 0.177597564757422
...
544 0.999011257908158
The expect number of rolls until six distinct faces appear in six consecutive rolls:
E = 83.2
(define (p7-1 n iter)
(local (success roll found)
(setq success 0)
(for (i 1 iter)
(setq roll (dice6-lst n))
(setq found nil)
(for (k 0 (- n 6) 1 found)
(if (= (sort (slice roll k 6)) '(1 2 3 4 5 6)) (setq found true))
)
(if found (++ success))
)
(div success iter)))
(time (for (n 6 20) (println n { } (p7-1 n 1e6))))
;-> 6 0.015528
;-> 7 0.028403
;-> 8 0.040712
;-> 9 0.05272
;-> 10 0.064964
;-> 11 0.077047
;-> 12 0.088167
;-> 13 0.100323
;-> 14 0.11155
;-> 15 0.123212
;-> 16 0.134386
;-> 17 0.145571
;-> 18 0.155803
;-> 19 0.167233
;-> 20 0.17747
;-> 70120.217
(time (println "544" { } (p7-1 544 1e6)))
;-> 544 0.999043
;-> 74985.561
(define (p7-2 iter)
(local (sum found trial roll nums)
(setq sum 0)
(for (i 1 iter)
(setq found nil)
; at least 6 dice are needed to find the sequence
(setq trial 5)
; first roll
(setq roll (dice6-lst 6))
(until found
; check unique digits on current sequence
(if (= (sort (copy roll)) '(1 2 3 4 5 6)) (setq found true))
(++ trial)
; new roll
(pop roll) (push (die6) roll -1)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p7-2 1e6)))
;-> 83.30385
;-> 37709.552
---------
Problem 8
---------
Person A rolls n dice and person B rolls m dice.
What is the probability that they have a common face showing (e.g., person A rolled a 2 and person B also rolled a 2, among all their dice)?
Solution =
n m prob.
1 1 0.1666666
1 2 0.3055555
2 2 0.5138888
1 3 0.4212962
2 3 0.6566358
3 3 0.7910236
1 4 0.5177469
2 4 0.7550154
3 4 0.8695773
4 4 0.9276084
5 5 0.9780956
6 6 0.9938971
(define (p8 n m iter)
(local (sum a b)
(setq sum 0)
(for (i 1 iter)
(setq a (dice6-lst n))
(setq b (dice6-lst m))
(if (intersect a b) (++ sum))
)
(div sum iter)))
(p8 1 1 1e6)
;-> 0.166295
(p8 1 2 1e6)
;-> 0.305687
(p8 2 2 1e6)
;-> 0.513849
(p8 1 3 1e6)
;-> 0.421542
(p8 2 3 1e6)
;-> 0.656901
(p8 3 3 1e6)
;-> 0.791544
(p8 1 4 1e6)
;-> 0.518101
(p8 2 4 1e6)
;-> 0.754937
(p8 3 4 1e6)
;-> 0.869145
(p8 4 4 1e6)
;-> 0.92736
(p8 5 5 1e6)
;-> 0.97803
(p8 6 6 1e6)
;-> 0.993827
---------
Problem 9
---------
On average, how many times must a 6-sided die be rolled until all sides appear at least once?
What about for an n-sided die?
Solution = 14.7
(define (p9 n iter)
(local (sum faces found trial)
(setq sum 0)
(for (i 1 iter)
(setq faces (dup nil (+ n 1)))
(setf (faces 0) true)
(setq found nil)
(setq trial 0)
(until found
(setf (faces (die n)) true)
(setq found (= (length (ref-all 'true faces)) (+ n 1)))
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p9 6 1e6)))
;-> 14.705402
;-> 5548.258
For a die with n faces:
prob = n*Sum[i=1..n](1/i)
(define (p9-exact n)
(let (sum 0)
(for (i 1 n)
(setq sum (add sum (div i)))
)
(mul n sum)))
(p9-exact 100)
;-> 518.737751763962
(time (println (p9 100 1e5)))
;-> 518.29085
;-> 104390.523
----------
Problem 10
----------
On average, how many times must a 6-sided die be rolled until all sides appear at least twice?
Solution = 24.1338692...
(define (p10 n iter)
(local (sum faces found trial)
(setq sum 0)
(for (i 1 iter)
(setq faces (dup 0 (+ n 1)))
(setf (faces 0) 2)
(setq found nil)
(setq trial 0)
(until found
(++ (faces (die n)))
; all numbers of faces are greater or equal 2?
(if (= (length (find-all 2 faces $it <=)) (+ n 1))
(setq found true)
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p10 6 1e6)))
;-> 24.135915
;-> 18805.147
Calculate the probability that after j rolls all sides have appeared at least twice.
Solution =
j prob
11 0
12 0.003438285
13 0.014899238
14 0.037661964
15 0.072748752
16 0.119155589
17 0.174576398
18 0.236147670
19 0.301007601
20 0.366633348
21 0.430995652
22 0.492589321
23 0.550391734
30 0.828548154
34 0.906280939
39 0.957121359
49 0.991461443
62 0.999009173
This problem is an example of what is often referred to as a Coupon Collector's problem.
(define (p10-2 n nroll iter)
(local (sum faces)
(setq sum 0)
(for (i 1 iter)
(setq faces (dup 0 (+ n 1)))
(setf (faces 0) 2)
(for (j 1 nroll) (++ (faces (die n))))
; all numbers of faces are greater or equal 2?
(if (= (length (find-all 2 faces $it <=)) (+ n 1)) (++ sum))
)
(div sum iter)))
(time (for (j 11 49) (println j { } (p10-2 6 j 1e6))))
;-> 11 0
;-> 12 0.003449
;-> 13 0.014824
;-> 14 0.037528
;-> 15 0.072657
;-> 16 0.119407
;-> 17 0.175054
;-> 18 0.235637
;-> 19 0.300605
;-> 20 0.36671
;-> 21 0.430828
;-> 22 0.493054
;-> 23 0.551049
;-> 24 0.604159
;-> 25 0.652677
;-> 26 0.695583
;-> 27 0.735347
;-> 28 0.770919
;-> 29 0.802058
;-> 30 0.828042
;-> 31 0.852119
;-> 32 0.872932
;-> 33 0.890399
;-> 34 0.906171
;-> 35 0.919331
;-> 36 0.93114
;-> 37 0.941177
;-> 38 0.94958
;-> 39 0.957342
;-> 40 0.96317
;-> 41 0.968737
;-> 42 0.973105
;-> 43 0.977453
;-> 44 0.980842
;-> 45 0.983586
;-> 46 0.985973
;-> 47 0.988242
;-> 48 0.98998
;-> 49 0.991628
;-> 202823.933
(time (println 62 { } (p10-2 6 62 1e6)))
;-> 62 0.999015
;-> 9847.644
-----------
Problema 11
-----------
On average, how many times must a pair of 6-sided dice be rolled until all sides appear at least once?
Solution = 7.59945887445...
(define (p11 n iter)
(local (sum faces found trial)
(setq sum 0)
(for (i 1 iter)
(setq faces (dup 0 (+ n 1)))
(setf (faces 0) 1)
(setq found nil)
(setq trial 0)
(until found
; rolls two dice
(++ (faces (die n)))
(++ (faces (die n)))
; all numbers of faces are greater or equal 2?
(if (= (length (find-all 1 faces $it <=)) (+ n 1))
(setq found true)
)
(++ trial)
)
(++ sum trial)
)
(div sum iter)))
(time (println (p11 6 1e6)))
;-> 7.599961
;-> 7398.658
----------
Problem 12
----------
Suppose we roll n dice.
What is the expected number of distinct faces that appear?
Solution =
n E
1 1
2 1.833333
3 2.527777
4 3.106481481
5 3.588734...
6 3.990612...
7 4.325510...
8 4.604591...
9 4.837159...
10 5.030966...
14 5.532680...
23 5.909430...
27 5.956322...
36 5.991535...
48 5.999050...
(define (p12 n iter)
(local (sum)
(setq sum 0)
(for (i 1 iter)
(++ sum (length (unique (dice6-lst n))))
)
(div sum iter)))
(time (for (n 1 48) (println n { } (p12 n 1e6))))
;-> 1 1
;-> 2 1.833859
;-> 3 2.528731
;-> 4 3.106721
;-> 5 3.588687
;-> 6 3.991302
;-> 7 4.325726
;-> 8 4.603786
;-> 9 4.837814
;-> 10 5.030227
;-> 11 5.19259
;-> 12 5.327828
;-> 13 5.438742
;-> 14 5.53269
;-> 15 5.61021
;-> 16 5.675945
;-> 17 5.730029
;-> 18 5.774289
;-> 19 5.81153
;-> 20 5.842915
;-> 21 5.869354
;-> 22 5.891612
;-> 23 5.909854
;-> 24 5.924193
;-> 25 5.936624
;-> 26 5.947644
;-> 27 5.956269
;-> 28 5.963438
;-> 29 5.969837
;-> 30 5.974563
;-> 31 5.978807
;-> 32 5.982656
;-> 33 5.985494
;-> 34 5.987834
;-> 35 5.989852
;-> 36 5.991497
;-> 37 5.992909
;-> 38 5.994039
;-> 39 5.994992
;-> 40 5.995903
;-> 41 5.996482
;-> 42 5.997188
;-> 43 5.997674
;-> 44 5.998017
;-> 45 5.99837
;-> 46 5.998611
;-> 47 5.99887
;-> 48 5.99909
;-> 223381.002
----------
Problem 13
----------
Suppose we roll n dice and keep the highest one.
What is the distribution of values?
Solution = the probability that, if n dice are rolled, the highest number to turn up will be k is
k^n - (k - 1)^n
prob(n,k) = -------------------
6^n
(define (p13-exact n k)
(div (sub (pow k n) (pow (sub k 1) n))
(pow 6 n)))
(p13-exact 7 3)
;-> 0.007355252629172382
(for (n 1 10)
(println "dice: " n)
(for (k 1 6)
;(println n { } k { } ((p13-exact n k))))
;(println n { } k { } ((p13-exact n k))))
;(println (format "%4d %4d %8.4f" n k (p13-exact n k)))))
(print (format " %2d %6.4f" k (p13-exact n k)))
)
(println))
;-> dice: 1
;-> 1 0.1667 2 0.1667 3 0.1667 4 0.1667 5 0.1667 6 0.1667
;-> dice: 2
;-> 1 0.0278 2 0.0833 3 0.1389 4 0.1944 5 0.2500 6 0.3056
;-> dice: 3
;-> 1 0.0046 2 0.0324 3 0.0880 4 0.1713 5 0.2824 6 0.4213
;-> dice: 4
;-> 1 0.0008 2 0.0116 3 0.0502 4 0.1350 5 0.2847 6 0.5177
;-> dice: 5
;-> 1 0.0001 2 0.0040 3 0.0271 4 0.1004 5 0.2702 6 0.5981
;-> dice: 6
;-> 1 0.0000 2 0.0014 3 0.0143 4 0.0722 5 0.2471 6 0.6651
;-> dice: 7
;-> 1 0.0000 2 0.0005 3 0.0074 4 0.0507 5 0.2206 6 0.7209
;-> dice: 8
;-> 1 0.0000 2 0.0002 3 0.0038 4 0.0351 5 0.1935 6 0.7674