Javatpoint Logo
Javatpoint Logo

Birthday Problem in Java

The birthday paradox, or dilemma, is a concept in probability theory. Although this does not constitute a paradox in the sense that it results in a logical contradiction, it is referred regarded as such since the mathematical reality goes against common sense: most people believe that the chance is far lower than 50%. refers to the likelihood that two randomly selected individuals who are in a group will have a birthday.

There is a greater than 50% chance that some pair of individuals in a group and with at least 23 randomly chosen people will have shared the same day of birth. The chance is more than 99% for groups of 57 or more persons, and it is 100% for groups of 366 or more (by the pigeonhole principle, ignoring leap years). The birthday attack is a well-known cryptography attack that is based on the mathematics behind such an issue.

How often people must be present in a room for the likelihood that at least two persons have the same birthday to be 100%?

Response: 367 (since there are 366 possible birthdays, including February 29).

The previous query was uncomplicated. Try the following query on your own.

How many individuals must be present in a room for the likelihood that at least two people share a birthday to reach 50%?

Response: 23

Surprisingly, the figure is relatively low. In reality, the probability can be reached with just 70 participants.

Let's talk about the broader formula.

What is the likelihood that two people out of n share the same birthday?

Let P represent the likelihood that two people inside a room with n share the same birthdate (same). P(different), which represents the likelihood that each person has a different birthdate, makes it simple to estimate P(same) in terms of P(different).

P(same) equals P - 1 (different)

It is possible to write P(different) as 1 x (364/365) x (363/365) x (362/365) x.... x (1 - (n-1)/365).

How did we arrive at the expression above?

To ensure that each birthday is unique, people can receive their birthdays in the following order, from first to last:

The first person's birthday can be any of the other 365.

The second individual should not share the same birthday as the first individual.

The third individual should not share the same birthday as the first two people.



The nth individual should be born on a different day than any of the (n-1) people who came before them in consideration.

a close-up of the above expression

Using Taylor's Series, the above statement can be roughly calculated.

gives an approximation of first order for ex for x << 1:

Set x = -a / 365 to apply that approximation to the initial expression produced for p(different). Thus,

The expression for p(different) above can be expressed as 1 x (1 - 1/365), 1 x (1 - 2/365), 1 x (1 - 3/365), 1 x.... x (1 - (n-1)/365).

The following results from writing 1 - a/365 as e-a/365.

Consequently, p(same) = 1 p (different)

p provides an even more basic approximation (same)

We can obtain the reverse formula by putting Log on both sides.

We may estimate how many persons there are with a given chance using the rough formula above. For instance, the find() function in Java returns the least n in which the probability exceeds the specified p.

Using a Rough Formula in Practice

An approximation of the population for a particular probability is provided in the code below.

File name:



Complexity analysis:

Time Complexity: O(log n)

Auxiliary Space: O(1)

File name:


Among the  no. of people out of which there is a 0.7 probability that two of them  have the same birthdays is 1

Time Complexity: O(log n)

Auxiliary Space: O(1)


Calculate the minimum number of independent members that a group must contain in order to have a more likely than not chance that at least two of those members have the same birthdate. Furthermore: Calculate the simulation to determine when there is a greater even than chance that for at least 3, 4, and 5 independent members of the group will share the same birthday. For the sake of simplicity, let's suppose that everyone is still alive.

Enhancement suggestions

  • Calculate the estimate's margin of error to make sure that estimate is correct to four decimal places.
  • Rather than conducting a thorough search, converge on the th solution using a root finding approach.
  • Congratulations (o) on choosing to use a programming language to prove the solution rather than using construction and simulation.

File name:


In a group of 23 people 2 independent people share a common birthday. ( 50.6)
In a group of 87 people 3 independent people share a common birthday. ( 50.4)
In a group of 187 people 4 independent people share a common birthday. ( 50.1)
In a group of 314 people 5 independent people share a common birthday. ( 50.2)


  1. The Birthday paradox is frequently mentioned in conjunction with hashing to highlight the significance of collision handling, even for a small collection of keys.
  2. Birthday Attack

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA