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
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
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.
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%.
6
6
500
Returns: 0.33373900738431983
5
100
199
Returns: 1.0
100
0
1000
Returns: 0.9940410733677595
100
100
1000
Returns: 1.0
2
0
1000
Returns: 0.0010000000000000009
8
9
861
Returns: 0.47633244112295925
3
6
864
Returns: 0.04462984396433478
8
0
991
Returns: 0.02792842028424869
4
7
837
Returns: 0.1037202933932595
7
3
989
Returns: 0.13973637985720944
4
5
998
Returns: 0.06468613308238735
11
9
854
Returns: 0.7289148212142105
10
8
278
Returns: 0.9625662974042857
9
4
941
Returns: 0.2966985662355618
7
5
862
Returns: 0.23979202287878243
11
6
856
Returns: 0.5820130494291855
11
3
988
Returns: 0.32801377007246546
4
0
816
Returns: 0.007336432130883197
86
2
981
Returns: 0.9999999992942917
6
2
822
Returns: 0.08797813234754603
11
2
896
Returns: 0.2678595856214231
8
3
814
Returns: 0.21733277735924805
7
7
899
Returns: 0.3030552979908263
5
7
812
Returns: 0.17232514501344431
11
5
933
Returns: 0.4885368069244831
8
1
394
Returns: 0.19475447453829398
11
6
892
Returns: 0.566378607805618
7
5
826
Returns: 0.24903972618388537
7
9
861
Returns: 0.382640009722343
4
83
860
Returns: 0.7711977404505264
9
9
975
Returns: 0.5201348983982399
8
9
801
Returns: 0.5023467256221588
7
9
907
Returns: 0.36674707411651153
10
8
984
Returns: 0.5565290064984671
9
6
846
Returns: 0.43632744590921924
6
4
887
Returns: 0.1432162498188312
10
5
870
Returns: 0.4444468814320526
44
7
990
Returns: 0.9999999734066081
7
8
861
Returns: 0.34927673223756683
10
9
842
Returns: 0.6595500742434905
5
83
812
Returns: 0.9442869497034398
6
39
844
Returns: 0.8075910238291373
10
5
964
Returns: 0.41066005841449194
11
8
886
Returns: 0.6721864010859104
10
9
893
Returns: 0.636635552776553
4
95
806
Returns: 0.8544239173631831
5
65
939
Returns: 0.8201458046269976
11
7
910
Returns: 0.6134195040375201
11
8
800
Returns: 0.7114733717745507
11
8
827
Returns: 0.6987598961905855
5
62
970
Returns: 0.7888974010485863
10
6
310
Returns: 0.8797673452083168
10
4
175
Returns: 0.9312335624381416
8
33
997
Returns: 0.8882583321970481
10
19
855
Returns: 0.9026870861597047
11
9
815
Returns: 0.746470063974957
2
100
100
Returns: 1.0
100
20
500
Returns: 1.0