Title: All Lotto Combinations Code
Question: To provide a fast algo with large numbers (45bit ore more) can turn into a very complicated debate.
Here's a solution for compiler or interpreter.
Answer:
You may know this:
Lotto combination 1-2-3-4-5-6 has the same chance of being drawn as any other lottery combination. But the combination 1,2,3,4,5,6 should not be played. It represents a weird arrangement of lottery numbers. Why?
If one hits the jackpot, you should be advised to never play the statistically dreadful 1,2,3,4,5,6!
1. If the lotto 6/45 has a total of 8,145,060 combinations, and only one is drawn as the winner, the probability is undeniably 1 / 8,145,060.
All shapes have, however, the same probability of appearance. Reality only has a different idea! There have been thousands of lotto drawings conducted worldwide. We should have seen by now a lottery combination of the type 1,2,3,4,5,6. There has been none so far or you know anyone?.
2. Furthermore, you can use the code to generate all lotto combinations in lexicographic order for any experiment or game. Beware! The output files can be extremely large, for 6/45 is about 240 MB!
One interesting point at all is the binominal coefficient. It means that you get the same amount of combinations with 6 or 39 tips (crosses) of 45!
Cause the mathematics shows
3. Combination (binominal coefficient)= nCr = n!/(k! * (n-k)!)
45! / 6! * 39!
so you can see 6 or 39 are interchangeable
The probabilities are equal, but the combinations are not equal.
-----------------------------------------------------------------
program All_LottoCombinations_bit_Power;
// shows the mighty performance of all solutions 6 of 45 by max & beat straehl
// ZWEIHOCHFUENFUNDVIERZIG = 35184372088832;
// time consuming is about 45 sec. (minutes in maXbox) without optimisation days!!
// number of lotto spheres are 45 with 6 crosses on it
{$APPTYPE CONSOLE}
uses SysUtils;
const NUMBER_OF_SPHERES = 45;
NUMBER_OF_BITS_ARRAY_UPPER_END = 255;
var counter, counterCopy, counterOffset, range: int64;
bitCounter, bitCount, bitPosition: integer;
combCounter: integer;
byteFragmentIndexOfCounter: integer;
positionOfLowestBitOutOfSix: integer;
numberOfBits: array[0..NUMBER_OF_BITS_ARRAY_UPPER_END] of byte;
before, after, beforePrint, afterPrint: TDateTime;
combstr: string;
lottoList: TStringList;
function powerOf2(exponent: integer): int64;
var shifter: int64;
counter: integer;
begin
shifter:= 1;
for counter:= 1 to exponent do
shifter:= shifter + shifter;
result:= shifter;
end;
procedure presetBitCountLookupTable(highestIndex: integer);
var arrayIndex: integer;
numberOfBitsSet: integer;
byteValue: integer;
begin
//preset bit count lookup table
for arrayIndex:= 0 to highestIndex do begin
byteValue:= arrayIndex;
numberOfBitsSet:= 0;
for bitCounter:= 1 to 8 do begin
if ((byteValue and 1) = 1) then
inc(numberOfBitsSet);
byteValue:= byteValue shr 1;
end;
numberOfBits[arrayIndex]:= numberOfBitsSet;
end;
end;
begin //main
try
presetBitCountLookupTable(NUMBER_OF_BITS_ARRAY_UPPER_END);
range:= powerOf2(NUMBER_OF_SPHERES);
lottoList:= TStringlist.create;
before:= Time;
Writeln('Lotto report start at: '+ FormatDateTime('hh:nn:ss.zzz',before));
Writeln('You get a file "All_lotto_combinations2.txt" of about 240 MByte!');
counter:= 0;
lottoList.add('All lotto combinations 6/ ' +IntToStr(NUMBER_OF_SPHERES));
lottoList.add('**********************************************************');
while counter range do begin //check cases
bitCount:= 0;
counterCopy:= counter;
for byteFragmentIndexOfCounter:= 0 to 5 do begin
bitCount:= bitCount+numberOfBits[counterCopy mod 256];
counterCopy:= counterCopy shr 8;
end;
if (bitCount=6) then begin
counterCopy:= counter;
positionOfLowestBitOutOfSix:= -1;
inc(combCounter);
//write('combination #' + inttostr(combinationCounter) +': ');
for bitCounter:=1 to NUMBER_OF_SPHERES do begin
if ((counterCopy and 1) = 1) then begin
//write(inttoStr(bitCounter) +' ');
combstr:= combstr+ ' '+inttoStr(bitCounter);
if (positionOfLowestBitOutOfSix= -1) then
positionOfLowestBitOutOfSix:= bitCounter;
end;
counterCopy:= counterCopy shr 1;
end;
//writeln(' ');
lottoList.add('# '+inttostr(combCounter)+': '+ combstr);
combstr:= '';
counterOffset:= 1;
bitPosition:= 1;
while (bitPositionpositionOfLowestBitOutOfSix) do begin
counterOffset:= counterOffset + counterOffset;
inc(bitPosition);
end;
counter:= counter + counterOffset;
end else
counter:= counter + 1;
end; //while
after:= Time;
beforePrint:= time;
Writeln('Save lotto file start: '+TimeToStr(beforePrint)+' '+DateToStr(date));
lottoList.SaveToFile('All_lotto_combinations2.txt');
lottoList.Free;
afterPrint:= time;
Writeln('Save lotto stopped at: '+TimeToStr(afterPrint)+' '+DateToStr(date));
Writeln('Save file lasted: '+FormatDateTime('hh:nn:ss.zzz',afterPrint-beforePrint));
Writeln('Lotto report calc end: '+ FormatDateTime('hh:nn:ss.zzz',after));
Writeln('Lotto calc lasted: '+FormatDateTime('hh:nn:ss.zzz',after-before));
Writeln('Lotto report total lasted: '+FormatDateTime('hh:nn:ss.zzz',afterPrint-before));
Writeln('6 of '+IntToStr(NUMBER_OF_SPHERES)+'are: ' +IntToStr(combCounter));
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.