fork download
  1. import java.math.BigInteger;
  2.  
  3. class Ideone
  4. {
  5. public static void main(String[] args)
  6. {
  7. System.out.println(hage(30));
  8. System.out.println(calcBirthdayProblemProbability(30));
  9. }
  10.  
  11. // 倍精度適当計算精度が足りない
  12. static double hage(int n)
  13. {
  14. double d = 1.0;
  15. for (int i = 0; i < n; i++)
  16. {
  17. d *= (365 - i) / 365d;
  18. }
  19. return 1.0 - d;
  20. }
  21.  
  22. static String calcBirthdayProblemProbability(int n)
  23. {
  24. BigInteger numerator = BigInteger.ONE;
  25. for (int i = 0; i < n; i++)
  26. numerator = numerator.multiply(BigInteger.valueOf(365 - i));
  27.  
  28. BigInteger denominator = BigInteger.valueOf(365).pow(n);
  29.  
  30. BigInteger probability = denominator.subtract(numerator);
  31.  
  32. BigInteger gcd = denominator.gcd(probability);
  33. return String.format("%d / %d", probability.divide(gcd), denominator.divide(gcd));
  34. }
  35. }
  36.  
Success #stdin #stdout 0.04s 320576KB
stdin
Standard input is empty
stdout
0.7063162427192688
9155292769247515688463664204581601330417463671119878183963440529188469 / 12962030625262519859085092767427552277425499342208403503894805908203125