program All_LottoCombinations_bit_Power; //********************************************************************************************* // To provide a fast algo with large numbers (45bit ore more) can turn into a complicated // debate. Here's a solution for compiler or interpreter. // shows a 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(change) {$APPTYPE CONSOLE} uses SysUtils, Classes; 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 6 of 45 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 from ' +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 (bitPosition