|
1 | 1 | use v6;
|
2 | 2 |
|
| 3 | +=begin pod |
| 4 | +
|
| 5 | +=TITLE Paper sheets of standard sizes: an expected-value problem |
| 6 | +
|
| 7 | +=AUTHOR Shlomi Fish |
| 8 | +
|
| 9 | +L<https://projecteuler.net/problem=151> |
| 10 | +
|
| 11 | +A printing shop runs 16 batches (jobs) every week and each batch requires a |
| 12 | +sheet of special colour-proofing paper of size A5. |
| 13 | +
|
| 14 | +Every Monday morning, the foreman opens a new envelope, containing a large |
| 15 | +sheet of the special paper with size A1. |
| 16 | +
|
| 17 | +He proceeds to cut it in half, thus getting two sheets of size A2. Then he |
| 18 | +cuts one of them in half to get two sheets of size A3 and so on until he |
| 19 | +obtains the A5-size sheet needed for the first batch of the week. |
| 20 | +
|
| 21 | +All the unused sheets are placed back in the envelope. |
| 22 | +
|
| 23 | +At the beginning of each subsequent batch, he takes from the envelope one |
| 24 | +sheet of paper at random. If it is of size A5, he uses it. If it is larger, |
| 25 | +he repeats the 'cut-in-half' procedure until he has what he needs and any |
| 26 | +remaining sheets are always placed back in the envelope. |
| 27 | +
|
| 28 | +Excluding the first and last batch of the week, find the expected number of |
| 29 | +times (during each week) that the foreman finds a single sheet of paper in |
| 30 | +the envelope. |
| 31 | +
|
| 32 | +Give your answer rounded to six decimal places using the format x.xxxxxx. |
| 33 | +
|
| 34 | +Expected result: 0.464398 |
| 35 | +
|
| 36 | +=end pod |
| 37 | + |
3 | 38 | my $sum = 0;
|
4 | 39 |
|
5 | 40 | # rec is short for "recurse".
|
@@ -36,3 +71,5 @@ sub rec($n, $d, @counts, $result)
|
36 | 71 | rec(1, 1, [1],0);
|
37 | 72 |
|
38 | 73 | say "Result == ", ($sum - 2).fmt("%.6f");
|
| 74 | + |
| 75 | +# vim: expandtab shiftwidth=4 ft=perl6 |
0 commit comments