pankaj
BAN USERI tried like :-
1.) check if sum of any face with itself n number of times becomes greater than or equal to x.
2.) if it becomes exactly equals to x, then negative cases are (i^n - 1).
3.) if it becomes greater than x, then negative cases are (i-1)^n + if (combinations between (i-1) and i give sum less than x).
private static float findProbability(int n, int m, int x) {
float prob = 0.0f;
float totalCombinations = (float) Math.pow(m, n);
System.out.println("total combs : " + totalCombinations);
for (int i = 1; i <= m; i++) {
if (i * n >= x) {
int negativeCount = 0;
if (i * n > x) {
int count = 0;
for (int j = 1; j < n; j++) {
if (((i - j) * j + i * (n - j)) < x) {
count++;
}
}
negativeCount = (int) Math.pow(i - 1, n) + count;
} else {
negativeCount = (int) Math.pow(i, n) - 1;
}
/* edit - start*/
int count1 = 0;
for (int j=1; j<=m-i; j++) {
for (int k=1; k<=i-2; k++) {
if ((i+j) * j + k < x) {
count1 ++;
}
}
}
negativeCount += count1 * n;
/* edit - end*/
System.out.println("Negative Cases : " + (negativeCount));
prob = (totalCombinations - negativeCount) / totalCombinations;
break;
}
}
return prob;
}
Also, i think the first example in the question is wrong, total number of cases for n=4 and m=2 becomes 2^4 = 16 , not 8.
- pankaj September 29, 2012
added code to handle the cases mentioned by you.
- pankaj September 30, 2012