http://blog.japan.cnet.com/kenn/archives/000711.htmlの記事の中に、
2人の人間が同じ誕生日である確率は約1/365であることは直観的に理解できる。 では、何人の人間が集まると同じ誕生日の人がいる確率が50%を超えるだろうか?
という問いかけがあります。答えは、23人ということですが実際にプログラムを作成し試してみました。
import java.math.*; public class BirthdayParadox { public static void main(String[] args){ double p = 0.0; // 確率 double bunshi; int n = 1; while (p <= 0.5) { bunshi = 365.0; for (int i = 1;i < n;i++) bunshi = bunshi * (365.0 - i); p = 1.0 - bunshi/Math.pow(365.0,(double)n); n++; } System.out.println(n - 1); System.out.println(p); } }
以下が実行結果です。
% java BirthdayParadox 23 0.5072972343239854