This code finds expectation and standard deviation of sum(x) of 6 digited base-49 numbers with all digits distinct.
The expectation is: \$\mu={\rm E[X]}=\frac1n\sum_{k=0}^{n}x_i=\frac{x_1+x_2+\ldots+x_n}{n}\$
whereas standard deviation is: \${\rm \sigma[X]}=\sqrt{{\rm E[(X-\mu)^2]}}=\sqrt{\frac1n[(x_1-\mu)^2+(x_2-\mu)^2+\ldots+(x_n-\mu)^2]}\$
package test;
import java.util.ArrayList;
import java.util.Arrays;
public class BallsSum {
final static int BALLS_DRAWN = 3;
final static int TOTAL_BALLS = 49;
public static void main(String args[]) {
long startTime = System.nanoTime();
int mNumberOfWays = 0;
int mTotalSum = 0;
ArrayList<Integer> mOneDrawPartialSums = new ArrayList<Integer>();
BallCombination mBallCombination = new BallsSum().new BallCombination();
int MAX_POSSIBLE_COMB = 1;
for (int i = 0; i < BALLS_DRAWN; i++) {
MAX_POSSIBLE_COMB *= (TOTAL_BALLS - i);
}
for (int i = 0; i < MAX_POSSIBLE_COMB; i++) {
++mNumberOfWays;
mOneDrawPartialSums.add(mBallCombination.getSum());
mTotalSum += mBallCombination.getSum();
mBallCombination.getNewCombination();
}
float mExpectationValue = mTotalSum / mNumberOfWays;
System.out.println("Expectation value is " + mExpectationValue);
int mStandardDeviationSum = 0;
for (int i = 0; i < mOneDrawPartialSums.size(); i++) {
mStandardDeviationSum += Math.pow(
(mOneDrawPartialSums.get(i) - mExpectationValue), 2);
}
float mStandardDeviation = (float) Math.sqrt(mStandardDeviationSum
/ mNumberOfWays);
System.out.println("Standard Deviation is " + mStandardDeviation);
long endTime = System.nanoTime();
System.out.println("Took "+(endTime - startTime) + " ns");
}
public class BallCombination {
int[] mCombination;
ArrayList<int[]> mFormedCombinations;
public BallCombination() {
this.mCombination = new int[BALLS_DRAWN];
for (int i = 0; i < BALLS_DRAWN; i++) {
this.mCombination[i] = i + 1;
}
this.mFormedCombinations = new ArrayList<int[]>();
this.mFormedCombinations.add(this.mCombination);
}
public void print() {
System.out.print("(");
for (int i = 0; i < BALLS_DRAWN; i++) {
System.out.print(this.mCombination[i]);
if (i != BALLS_DRAWN - 1) {
System.out.print(",");
}
}
System.out.println(")");
}
public boolean getNewCombination() {
for (int i = 0; i < BALLS_DRAWN; i++) {
if (this.mCombination[i] < TOTAL_BALLS) {
++this.mCombination[i];
// Debug printing System.out.print("newComb:");
print();
if (isUnique(this.mCombination)) {
mFormedCombinations.add(mCombination);
return true;
}
} else {
this.mCombination[i] = 1;
}
}
return false;
}
private boolean isUnique(int[] pCombination) {
if (containsDuplicate(pCombination)) {
return false;
}
for (int i = 0; i < mFormedCombinations.size(); i++) {
if (sort(mFormedCombinations.get(i)).equals(sort(pCombination))) {
return false;
}
}
return true;
}
private int[] sort(int[] pArray) {
Arrays.sort(pArray);
return pArray;
}
private boolean containsDuplicate(int[] pCombination) {
for (int i = 0; i < pCombination.length; i++) {
for (int j = i + 1; j < pCombination.length; j++) {
if (pCombination[i] == pCombination[j]) {
return true;
}
}
}
return false;
}
public int getSum() {
int sum = 0;
for (int i = 0; i < this.mCombination.length; i++) {
sum += this.mCombination[i];
}
return sum;
}
}
}
I'm not sure if this is correct way to do this because i want to calculate it for BALLS_DRAWN=6
that may take hours with this code because exponential interpolation of the time required for the task from 1,2,3 and 4 balls drawn is 6 hrs, see this and this, but console shows out of memory error for 5 only. :D