Browse Source

Merge branch 'master' of http://git.yoqi.me:3000/lyq/yunyin

liuyuqi-dellpc 7 years ago
parent
commit
c6c8500dbb
3 changed files with 87 additions and 0 deletions
  1. 54 0
      Test.java
  2. 21 0
      birthday.py
  3. 12 0
      main.m

+ 54 - 0
Test.java

@@ -0,0 +1,54 @@
+package me.yoqi;
+
+import java.math.BigInteger;
+
+public class Test {
+
+	public static void main(String args[]) {
+		int init = 25;
+		double pk = 0;
+		for (int n = 3; n < init; n++) {
+			BigInteger nn = new BigInteger(String.valueOf(Math.pow(365, n)));
+			long temp = (combination(365, n).multiply(factorial(n)).divide(nn)).longValue();
+			pk = 1 - temp;
+			if (pk >= 0.5) {
+				System.out.println("最小K:" + n);
+				break;
+			}
+		}
+		System.out.print(factorial(25));
+	}
+
+	/**
+	 * 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
+	 * 
+	 * @param n
+	 * @return
+	 */
+	private static BigInteger factorial(int n) {
+		BigInteger nn=new BigInteger(Integer.toString(n));
+		return (BigInteger) ((n > 1) ? factorial(n - 1).multiply(nn) : 1);
+	}
+
+	/**
+	 * 计算排列数,即A(n, m) = n!/(n-m)!
+	 * 
+	 * @param n
+	 * @param m
+	 * @return
+	 */
+	public static BigInteger arrangement(int n, int m) {
+		return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)) : 0);
+	}
+
+	/**
+	 * 计算组合数,即C(n, m) = n!/((n-m)! * m!)
+	 * 
+	 * @param n
+	 * @param m
+	 * @return
+	 */
+	public static BigInteger combination(int n, int m) {
+		return (BigInteger) ((n >= m) ? factorial(n).divide(factorial(n - m)).divide(factorial(m)) : 0);
+	}
+}

+ 21 - 0
birthday.py

@@ -0,0 +1,21 @@
+#coding=utf-8
+'''
+Created on 2017年6月9日
+@vsersion:python3.6
+@author: liuyuqi
+'''
+from scipy.special import comb, perm
+from math import factorial
+#1-C(365,n)n!/(365)^n
+init=365;
+k=0;
+for n in range(4,init):
+    pk=1-comb(365,n)*factorial(n)/pow(365,n);
+    if pk>=0.5 :
+        k=n;
+        break;
+print(k)
+# perm(m, n) 排列数
+# comb(3, 2) 组合数
+# pow(a,b) n次方
+# factorial (x)阶乘

+ 12 - 0
main.m

@@ -0,0 +1,12 @@
+%1-C(365,n)n!/(365)^n
+init=365;
+k=0;
+for n=4:init
+    pk=1-nchoosek(365,n)*factorial(n)/power(365,n);
+    %pk=1-factorial(364)/(power(365,n)*factorial(n-2));
+    if pk>=0.5
+     k=n;
+     break;
+    end
+end
+k