Question
IN PYTHON The birthday problem concerns the probability that in a group of people, two people will have the same birthday. Of course, as the
IN PYTHON
The "birthday problem" concerns the probability that in a group of people, two people
will have the same birthday. Of course, as the size of the group increases, the
probability of two people sharing the same birthday increases as well. Surprisingly, in
a
group of just 23 people, there is a 50% probability that at least two people will have
the same birthday!
The probability of two people in a group of
n
people having the same birthday may be
calculated as follows:
p(n) = 1
-
365/365 * 364/365 * 363/365 *
... * (365
-
n + 1)/365
For example, in a group of 4 people, the probability of two people having the same
birthday would be calculated as follows:
p(4) = 1
-
365/365 * 364/365 * 363/365 * (365
-
4 + 1)/365
= 1
-
365/365 * 364/365 * 363/365 * 362/365
= .016355912466550215
Requirements
Write a program named
Birthdays
.py
that prompts the user for the minimum and
maximum number of people and then outputs the probability that two people will
have the same birthday for every number between the minimum and maximum. The
probability values should be calculated using the above f
ormula, and displayed with
four decimal
places and rounded to the nearest decimal place, as shown below.
For example
(
the
red
ink highlights the user input, the
blue
ink highlights the expected
output
)
,
Minimum number of people:
4
Maximum number of peo
ple:
25
Probability of
Two People Having
Number of People the Same Birthday
----------------
-----------------
4 0.0164
5 0.0271
6 0.0405
7 0.0562
8 0.0743
9 0.0946
10 0.1169
11 0.1411
12 0.1670
13 0.1944
14 0.2231
15 0.2529
16 0.2836
17 0.3150
18 0.3469
19 0.3791
20 0.4114
21 0.4437
22 0.4757
23
0.5073
24 0.5383
25 0.5687
Design
Your
program
must prompt the user for the input values and use the method given below to
calculate each probability.
Your program
will output each probability with 4 decimal
places and
rounded to the nearest decimal place. The output must be displayed in columns as shown above.
You
should define a
calculate_probability
function and
documented as shown below:
def calculate_probability(n):
"""
Calculates probability that two people in a group of people will have the
same birthday
:param n: integer, number of people
:return: float, probability that two people will have the same birthday
"""
Implementation
Use the following named
(
constant
) variable
in your program rather than a
hard
-
wired
number
365
.
o
DAYS_PER_YEAR = 365;
Your
program
must use a loop
. The method above should be called from within the loop.
Your methods must return the correct probability value as a
double for all reasonable
input values, not just the ones used to create the table above.
Output the probability values with 4 decimal places and rounded to the last decimal place
by using a prin
t
statement to format the value. For example, the following
statement
formats an amount so that it is rounded, has 4 decimal places, and takes up a total of 20
spaces:
print("%20.4f" % 3.1415926)
You can also use the print
statement to help with formatting the number of people for each
probability value
.
Study
the code below to learn more about string formatting.
print("%10d, %10d" % (55, 777))
print("%10.4f, %10.4f" % (55.55, 0.7777777777777777))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started