Statistics

Problem Statement for "NearBirthdays"

Problem Statement

If more than 23 people are selected at random, the chance that two of them have birthdays on the same day is more than 50%. While this may sound counterintuitive, it is fairly easy to verify that this indeed is the case. Furthermore, only 14 people are needed for there to be more than a 50% chance that at least two of them have birthdays within one day of each other.

Create a class NearBirthdays containing the method probability which calculates the probability that at least two people among noPeople have birthdays within withinDays days (inclusive) of each other. The method should take as input an int noPeople, an int withinDays, and an int daysInAYear, the number of days in a year, and return the probability as a double. Assume that each day in a year is equally likely to be a birthday for someone.

Definition

Class:
NearBirthdays
Method:
probability
Parameters:
int, int, int
Returns:
double
Method signature:
double probability(int noPeople, int withinDays, int daysInAYear)
(be sure your method is public)

Notes

  • The last day of a year is within one day of the first day of a year.
  • A solution will be judged correct if the absolute error is less than 1e-9.

Constraints

  • noPeople will be between 2 and 100, inclusive.
  • withinDays will be between 0 and 100, inclusive.
  • daysInAYear will be between 100 and 1000, inclusive.

Examples

  1. 2

    99

    200

    Returns: 0.995

    If the first person's birthday is on day 1 in the 200 day long year, the only time the second person's birthday isn't within 99 days is if he has a birthday on day 101. Hence the probability that both persons have birthdays within 99 days of each other is 199/200 = 0.995.

  2. 30

    0

    365

    Returns: 0.7063162427192686

    This is a typical school class version: in a school class with 30 people, the likelihood that two of them are born on the same day is roughly 70%.

  3. 6

    6

    500

    Returns: 0.33373900738431983

  4. 5

    100

    199

    Returns: 1.0

  5. 100

    0

    1000

    Returns: 0.9940410733677595

  6. 100

    100

    1000

    Returns: 1.0

  7. 2

    0

    1000

    Returns: 0.0010000000000000009

  8. 8

    9

    861

    Returns: 0.47633244112295925

  9. 3

    6

    864

    Returns: 0.04462984396433478

  10. 8

    0

    991

    Returns: 0.02792842028424869

  11. 4

    7

    837

    Returns: 0.1037202933932595

  12. 7

    3

    989

    Returns: 0.13973637985720944

  13. 4

    5

    998

    Returns: 0.06468613308238735

  14. 11

    9

    854

    Returns: 0.7289148212142105

  15. 10

    8

    278

    Returns: 0.9625662974042857

  16. 9

    4

    941

    Returns: 0.2966985662355618

  17. 7

    5

    862

    Returns: 0.23979202287878243

  18. 11

    6

    856

    Returns: 0.5820130494291855

  19. 11

    3

    988

    Returns: 0.32801377007246546

  20. 4

    0

    816

    Returns: 0.007336432130883197

  21. 86

    2

    981

    Returns: 0.9999999992942917

  22. 6

    2

    822

    Returns: 0.08797813234754603

  23. 11

    2

    896

    Returns: 0.2678595856214231

  24. 8

    3

    814

    Returns: 0.21733277735924805

  25. 7

    7

    899

    Returns: 0.3030552979908263

  26. 5

    7

    812

    Returns: 0.17232514501344431

  27. 11

    5

    933

    Returns: 0.4885368069244831

  28. 8

    1

    394

    Returns: 0.19475447453829398

  29. 11

    6

    892

    Returns: 0.566378607805618

  30. 7

    5

    826

    Returns: 0.24903972618388537

  31. 7

    9

    861

    Returns: 0.382640009722343

  32. 4

    83

    860

    Returns: 0.7711977404505264

  33. 9

    9

    975

    Returns: 0.5201348983982399

  34. 8

    9

    801

    Returns: 0.5023467256221588

  35. 7

    9

    907

    Returns: 0.36674707411651153

  36. 10

    8

    984

    Returns: 0.5565290064984671

  37. 9

    6

    846

    Returns: 0.43632744590921924

  38. 6

    4

    887

    Returns: 0.1432162498188312

  39. 10

    5

    870

    Returns: 0.4444468814320526

  40. 44

    7

    990

    Returns: 0.9999999734066081

  41. 7

    8

    861

    Returns: 0.34927673223756683

  42. 10

    9

    842

    Returns: 0.6595500742434905

  43. 5

    83

    812

    Returns: 0.9442869497034398

  44. 6

    39

    844

    Returns: 0.8075910238291373

  45. 10

    5

    964

    Returns: 0.41066005841449194

  46. 11

    8

    886

    Returns: 0.6721864010859104

  47. 10

    9

    893

    Returns: 0.636635552776553

  48. 4

    95

    806

    Returns: 0.8544239173631831

  49. 5

    65

    939

    Returns: 0.8201458046269976

  50. 11

    7

    910

    Returns: 0.6134195040375201

  51. 11

    8

    800

    Returns: 0.7114733717745507

  52. 11

    8

    827

    Returns: 0.6987598961905855

  53. 5

    62

    970

    Returns: 0.7888974010485863

  54. 10

    6

    310

    Returns: 0.8797673452083168

  55. 10

    4

    175

    Returns: 0.9312335624381416

  56. 8

    33

    997

    Returns: 0.8882583321970481

  57. 10

    19

    855

    Returns: 0.9026870861597047

  58. 11

    9

    815

    Returns: 0.746470063974957

  59. 2

    100

    100

    Returns: 1.0

  60. 100

    20

    500

    Returns: 1.0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: