Test.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package me.yoqi;
  2. import java.math.BigInteger;
  3. public class Test {
  4. public static void main(String args[]) {
  5. int init = 25;
  6. double pk = 0;
  7. for (int n = 3; n < init; n++) {
  8. BigInteger nn = new BigInteger(String.valueOf(Math.pow(365, n)));
  9. long temp = (combination(365, n).multiply(factorial(n)).divide(nn)).longValue();
  10. pk = 1 - temp;
  11. if (pk >= 0.5) {
  12. System.out.println("最小K:" + n);
  13. break;
  14. }
  15. }
  16. System.out.print(factorial(25));
  17. }
  18. /**
  19. * 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
  20. *
  21. * @param n
  22. * @return
  23. */
  24. private static BigInteger factorial(int n) {
  25. BigInteger nn=new BigInteger(Integer.toString(n));
  26. return (BigInteger) ((n > 1) ? factorial(n - 1).multiply(nn) : 1);
  27. }
  28. /**
  29. * 计算排列数,即A(n, m) = n!/(n-m)!
  30. *
  31. * @param n
  32. * @param m
  33. * @return
  34. */
  35. public static BigInteger arrangement(int n, int m) {
  36. return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)) : 0);
  37. }
  38. /**
  39. * 计算组合数,即C(n, m) = n!/((n-m)! * m!)
  40. *
  41. * @param n
  42. * @param m
  43. * @return
  44. */
  45. public static BigInteger combination(int n, int m) {
  46. return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)).divide(factorial(m)) : 0);
  47. }
  48. }