TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: A random maths problem we started discussing at work

8 pointsby cleisover 11 years ago
How many people would you need in a group to make it likely that at least one member had a birthday on each of 365 days of the year?<p>Let&#x27;s ignore leap years and define likely as &gt;50% probability.

7 comments

wnoiseover 11 years ago
This is the coupon collector&#x27;s problem.<p><a href="http://en.wikipedia.org/wiki/Coupon_collector%27s_problem" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Coupon_collector%27s_problem</a><p>The formula in the article (1&#x2F;2 + n*gamma + n log n) gives 2365 for the expected number, but this is different than &quot;at least 50% chance&quot;.
评论 #6897606 未加载
评论 #6896571 未加载
DanielStraightover 11 years ago
I have no answer for you. I mean... the solution seems to be 2121 or 2122 (no, it isn&#x27;t, see below) determined by brute force, but I don&#x27;t know why.<p>But, it&#x27;s worth noting that if this interests you, Project Euler almost certainly will as well and is worth checking it out if you don&#x27;t already know about it:<p><a href="http://projecteuler.net/" rel="nofollow">http:&#x2F;&#x2F;projecteuler.net&#x2F;</a><p>EDIT: So if you&#x27;re wondering why I got a different brute force result from other users, it&#x27;s because I had a stupid bug. I was looking at the ratio of meeting the criteria to the ratio of not meeting it, not the ratio of meeting it to total.
metaphyzeover 11 years ago
Brute force gives me about 2287&#x2F;2288. Trying each number 1,000,000 times (distribute X balls in 365 bins, count number times all bins are filled) so it seems reliable, but maybe I have bug somewhere:<p>import java.util.Random;<p>public class Birthday {<p>private static Random rand = new Random(System.currentTimeMillis());<p>public static void main(String... args) {<p><pre><code> final int n = 365; boolean[] bins = new boolean[n]; final float maxNumberOfAttempts = 1000000; for (int numberOfBalls = 2285; numberOfBalls &lt;= 2400; ++numberOfBalls) { int attempts = 0; int numberOfTimesAllBinsFilled = 0; while (attempts &lt; maxNumberOfAttempts) { if (distribute_X_Balls_in_N_bins_randomly_and_return_the_number_of_filled_bins( numberOfBalls, bins) == n) { numberOfTimesAllBinsFilled++; } ++attempts; } float prob = numberOfTimesAllBinsFilled &#x2F; maxNumberOfAttempts; System.out.println((prob &gt; .5 ? &quot;PASSED:&quot; : &quot;FAILED:&quot;) + numberOfBalls + &quot;:&quot; + prob); } } public static int distribute_X_Balls_in_N_bins_randomly_and_return_the_number_of_filled_bins( int x, boolean[] bins) { for (int inx = 0; inx &lt; bins.length; ++inx) { bins[inx] = false; } int whichBin = 0; int total = 0; while (--x &gt;= 0) { whichBin = rand.nextInt(bins.length); if (!bins[whichBin]) { bins[whichBin] = true; ++total; if (total == bins.length) { return total; } } } return total; } }</code></pre>
评论 #6897039 未加载
评论 #6905421 未加载
ddedover 11 years ago
Here&#x27;s my take:<p>Let n be the number of people needed. Probability that a person&#x27;s birthday is not today: 364&#x2F;365<p>Prob. that no one&#x27;s birthday is today: (364&#x2F;365)^n<p>Prob. that someone&#x27;s birthday is today: 1 - (364&#x2F;365)^n<p>Prob. that someone&#x27;s birthday covers every day: (1 - (364&#x2F;365)^n)^365<p>Setting equal to 0.5 and solving, I get n==2284, which is close to metaphyze&#x27;s brute force number.
darkxanthosover 11 years ago
Here&#x27;s a link to the wikipedia entry on the birthday problem.<p><a href="http://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Birthday_problem</a>
评论 #6896137 未加载
minussohnover 11 years ago
are you too stupid or too lazy to use a proper web search engine?
chaoticgeekover 11 years ago
This solves the problem for you. <a href="http://en.wikipedia.org/wiki/Birthday_problem" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Birthday_problem</a><p>23 people is a 50% chance, 57 is a 99% chance, 367 is 100% chance (including leap years).
评论 #6896139 未加载