Birthday problem: Difference between revisions

m
m (→‎{{header|C}}: fix typo)
m (→‎{{header|Perl 6}}: Limit to 365.)
Line 988:
For a start, we can show off how to get the exact solution. If we pick n people, the total number of possible arrangements of birthdays is <tt>365<sup>n</sup></tt>. Among those possibilities, there are <tt>C<sup>n</sup><sub>365</sub></tt> where all birthdays are different. For each of these, there are <tt>n!</tt> possible ways to arrange the n people. So the solution is <tt>1 - n!C<sup>n</sup><sub>365</sub>/365<sup>n</sup></tt>, which in Perl 6 can be written:
 
<lang perl6>say "$_ :", 1 - combinations(365, $_)/365**$_ * [*] 1..$_ for ^Inf365</lang>
{{out}}
<pre>0 : 0
Line 1,009:
}
 
for 2 .. Inf365 -> $n {
printf "%3d people, theory: %.4f, simulation: %.4f\n",
$n, theory($n), simulation(number-of-people => $n);
10,333

edits