[英]Math in Java(Combinatorics)
My math formula is: 我的数学公式是: In this case X = N; 在这种情况下,X = N; Y = L;U = K; Y = L; U = K;
public class Play {
public static void main(String args[]) {
//n!(n−k−1)!
int n = 10;
int k =2;
int l = 12;
long result;
result = (calculaFator(n) / calculaFator(n-k-1));
result= (long) (result * Math.pow((n-k),(l-k)-1));
System.out.println(result);
}
public static long calculaFator(long x) {
long f = x;
while (x > 1) {
f = f * (x - 1);
x--;
}
return f;
}
}
It should be 721599986, but it is giving me 96636764160 应该是721599986,但它给了我96636764160
I have some samples: 我有一些样本:
With n=10, k=2, l=12 it should be 721599986 在n = 10,k = 2,l = 12的情况下应该是721599986
With n=10, k=2, l=16 it should be 626284798 在n = 10,k = 2,l = 16的情况下应该是626284798
With n=10, k=1, l=20 it should be 674941304 在n = 10,k = 1,l = 20的情况下应该是674941304
With n=5, k=2, l=8 it should be 10800 在n = 5,k = 2,l = 8的情况下应为10800
The java codes is working according to your stated formula. Java代码根据您指定的公式运行。 It seems like the formula is wrong rather than the codes. 看来公式是错误的,而不是代码。 (or expected results or your x,u,y mapping to n,l,k is incorrect?) (或预期结果,或者您映射到n,l,k的x,u,y不正确?)
int x = 10;
int u = 2;
int y = 12;
long numerator = calculaFator(x);
long denominator = calculaFator(x - u - 1);
int xu1 = x - u - 1;
long result = numerator / denominator;
System.out.println();
System.out.println(x + "!= numerator: " + numerator); //10!= numerator: 3_628_800
System.out.println(xu1 + "!= denominator: " + denominator); //7!= denominator: 5_040
System.out.println("result1: " + result); //result1: 720 (correct)
int xu = x - u;
int yu1 = y - u - 1;
double remainderPlaylist = Math.pow(xu, yu1);
System.out.println(xu + "^" + yu1 + " = " + remainderPlaylist);//8^9 = 1.34217728E8
System.out.println(xu + "^" + yu1 + " = " + (long) remainderPlaylist);//8^9 = 134_217_728 (correct)
long mul = (long) (result * remainderPlaylist);
System.out.println(result + "x" + (long)remainderPlaylist + " = " + mul); //720x134_217_728 = 96_636_764_160 (mathematically correct)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.