Birthday problem: Difference between revisions
Content added Content deleted
m (→version 1: changed whitespace and some comments; added wording to the REXX section header.) |
|||
Line 1,669: | Line 1,669: | ||
461 49316 50684 50.68400% <- |
461 49316 50684 50.68400% <- |
||
462 49121 50879 50.87900% <-</pre> |
462 49121 50879 50.87900% <-</pre> |
||
=={{header|Ruby}}== |
|||
{{trans|Java}} |
|||
<lang ruby>def equalBirthdays(nSharers, groupSize, nRepetitions) |
|||
eq = 0 |
|||
for i in 1 .. nRepetitions |
|||
group = [0] * 365 |
|||
for j in 1 .. groupSize |
|||
group[rand(group.length)] += 1 |
|||
end |
|||
eq += group.any? { |n| n >= nSharers } ? 1 : 0 |
|||
end |
|||
return (eq * 100.0) / nRepetitions |
|||
end |
|||
def main |
|||
groupEst = 2 |
|||
for sharers in 2 .. 5 |
|||
# Coarse |
|||
groupSize = groupEst + 1 |
|||
while equalBirthdays(sharers, groupSize, 100) < 50.0 |
|||
groupSize += 1 |
|||
end |
|||
# Finer |
|||
inf = (groupSize - (groupSize - groupEst) / 4.0).floor |
|||
for gs in inf .. groupSize + 999 |
|||
eq = equalBirthdays(sharers, groupSize, 250) |
|||
if eq > 50.0 then |
|||
groupSize = gs |
|||
break |
|||
end |
|||
end |
|||
# Finest |
|||
for gs in groupSize - 1 .. groupSize + 999 |
|||
eq = equalBirthdays(sharers, gs, 50000) |
|||
if eq > 50.0 then |
|||
groupEst = gs |
|||
print "%d independant people in a group of %s share a common birthday. (%5.1f)\n" % [sharers, gs, eq] |
|||
break |
|||
end |
|||
end |
|||
end |
|||
end |
|||
main()</lang> |
|||
{{out}} |
|||
<pre>2 independant people in a group of 23 share a common birthday. ( 50.5) |
|||
3 independant people in a group of 90 share a common birthday. ( 53.3) |
|||
4 independant people in a group of 187 share a common birthday. ( 50.3) |
|||
5 independant people in a group of 313 share a common birthday. ( 50.3)</pre> |
|||
=={{header|SQL}}== |
=={{header|SQL}}== |