誕生日のパラドックス
問題
あるクラスにはn人が存在します。
n人のうち、少なくとも2人以上同じ誕生日がいる確率pを求めましょう。
・確率pの求め方
\( p = 1 - \left( \frac{364}{365} \times \frac{363}{365} \times \frac{362}{365} \times \cdots \times \frac{365-n+1}{365} \right) \)
例
クラスの人数を入力してください >23 同じ誕生日が2人以上いる確率は約0.507%
クラスの人数を入力してください >10 同じ誕生日が2人以上いる確率は約0.117%
解答例
using System; namespace BirthDay { class Program { static void Main(string[] args) { Console.WriteLine("クラスの人数を入力してください"); int menberNum = int.Parse(Console.ReadLine()); People p = new People(menberNum); Console.WriteLine("同じ誕生日が2人以上いる確率は約{0}%", Math.Round(p.ProbPair,3)); } } class People { public int Number; public double ProbPair; public People(int n) { this.Number = n; this.ProbPair = this.ProbabirityOfPair(); } private double ProbabirityOfPair() { return 1.0 - P(); } private double P() { double f = 1; for (int i = 365; i >= (365 - this.Number + 1); i--) { f *= i / 365.0; } return f; } } }