The Birthday Problem (2)

So, this is an improved version of my previous Birthday Problem. 

The major improvement was that I used “DeleteDuplicates” instead of “Union”. 

The difference in computation time is quite remarkable. DeleteDuplicates took 29 seconds, while Union took about 72 seconds. 


f[k_] := (
  count = 0;
  repeat = 10000;
  Do[
   a = RandomInteger[{1, 365}, k];
   b = DeleteDuplicates[a];
   If[Length[a] != Length[b], count++
    ]
   , {i, 1, repeat}
   ];
  prob = N[count/repeat];
  Return[prob];
  )
list = Table[f[i], {i, 1, 365}];
ListPlot[list]

So, yes! Sleep is help to gain new perspective about how to do things.

And yes, there is always room for improvement. Maybe the test using Length isn’t the most efficient. I shall explore more about this next time. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s