Using C#, I need to find out all the combinations of the possible outcomes of UK National Lottery (excluding Bonus Number). So I am trying to get all the combination of six numbers from the unique combinations of Numbers 1 to 59. Each combination consists of non-repetitive numbers and order is irrelevant.
The output of all the valid combinations are:
1 2 3 4 5 6
1 2 3 4 5 7
... so on
1 2 3 4 5 59
1 2 3 4 6 7
... so on
53 54 55 56 57 58
53 54 55 56 57 59
54 55 56 57 58 59
As there are more than 45 million combinations, I have tried to implement it with one jagged array as result set but it is throwing out of memory exception.
So I have split it in two Result sets as in the below code but it is still throwing the same exception.
How can I get all the combinations in either two result sets or one single result set?
In the below code, I have used Jagged Arrays but the result set can be of any object type.
I obviously need the algorithm with the best performance with regard to both Time and Space complexity.
Thanks for your help in advance.
private void GetAllCombinationsForSixNumbers(out int[][] arrayOfAllCombinationsFirstSet, out int[][] arrayOfAllCombinationsSecondSet)
{
arrayOfAllCombinationsFirstSet = new int[25000000][];
arrayOfAllCombinationsSecondSet = new int[25000000][];
for (int eachArray = 0; eachArray < arrayOfAllCombinationsFirstSet.Length; eachArray++)
{
arrayOfAllCombinationsFirstSet[eachArray] = new int[6];
arrayOfAllCombinationsSecondSet[eachArray] = new int[6];
}
int arrayCurrentRowIndex = 0, arrayCurrentRowIndexForSecondArray = 0;
for (int firstNumber = 1; firstNumber < 59; firstNumber++)
{
for (int secondNumber = firstNumber + 1; secondNumber <= 59; secondNumber++)
{
for (int thirdNumber = secondNumber + 1; thirdNumber <= 59; thirdNumber++)
{
for (int fourthNumber = thirdNumber + 1; fourthNumber <= 59; fourthNumber++)
{
for (int fifthNumber = fourthNumber + 1; fifthNumber <= 59; fifthNumber++)
{
for (int sixthNumber = fifthNumber + 1; sixthNumber <= 59; sixthNumber++)
{
if (arrayCurrentRowIndex < arrayOfAllCombinationsFirstSet.Length)
{
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][0] = firstNumber;
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][1] = secondNumber;
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][2] = thirdNumber;
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][3] = fourthNumber;
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][4] = fifthNumber;
arrayOfAllCombinationsFirstSet[arrayCurrentRowIndex][5] = sixthNumber;
arrayCurrentRowIndex++;
}
else
{
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][0] = firstNumber;
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][1] = secondNumber;
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][2] = thirdNumber;
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][3] = fourthNumber;
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][4] = fifthNumber;
arrayOfAllCombinationsSecondSet[arrayCurrentRowIndexForSecondArray][5] = sixthNumber;
arrayCurrentRowIndexForSecondArray++;
}
}
}
}
}
}
}
}