Primes whose sum of digits is 25: Difference between revisions

m
→‎{{header|Pascal}}: changed namings and comments
m (forgot </pre>)
m (→‎{{header|Pascal}}: changed namings and comments)
Line 650:
 
=={{header|Pascal}}==
added only strechted goal.Generating the combination of the digits for the numbers byand permutation.Sameafterwards resultgenerating asthe [[go]].runtimePermutations reducedwith fromsome 5.8sidentical to 0.4s<BR>elements]]
<BR>Same count of generated numbers as in [[go]] .Runtime for only generating the numbers reduced from 5.8s to 0.4s<BR>
I don't know why the gmp test is so much faster and why there is one more probably prime.<BR>
<lang pascal>
Added sum to n*9 => no primes ;-)
program Perm5aus8;
<lang pascal>program Perm5aus8;
//formerly roborally take 5 cards out of 8
{$IFDEF FPC}
{$mode Delphi}
Line 664 ⟶ 666:
gmp;
const
cTotalSum = 2563;
 
cMaxCardsOnDeck = cTotalSum;//8
CMaxCardsUsed = cTotalSum;//5
 
type
Line 675 ⟶ 677:
tDiffCardCount = 0..9;
 
tMengenElemtSetElem = record
Elemcount : tDeckIndex;
Elem : tDiffCardCount;
end;
 
tMengetSet = record
RestMengeRemSet : array [low(tDiffCardCount)..High(tDiffCardCount)] of tMengenElemtSetElem;
MaxUsedIdx,
TotElemCnt : word;
end;
 
tRestmengentRemainSet = array [low(tSequenceIndex)..High(tSequenceIndex)+1] of tMengetSet;
 
tCardSequence = array [low(tSequenceIndex)..High(tSequenceIndex)] of tDiffCardCount;
Line 695 ⟶ 697:
 
var
RemainSets : tRemainSet;
// globale Variable, um Stelle der Änderung gegenüber der
// Vorgänger permutation festzuhalten
Restmengen : tRestmengen;
TotalUsedDigits : array[tDeckIndex] of Int32;
ManifoldOfDigit : array[tDiffCardCount] of Int32;
CardSequence : tCardSequence;
CardString : string[cMaxCardsOnDeck+3]AnsiString;
 
Count: NativeInt;
Line 708:
 
//*****************************************************************************
procedure MengeInitSetInit(var ioMenge:tMengetSet);
var
i : integer;
Line 716:
MaxUsedIdx := 0;
For i := Low(tDiffCardCount) to High(tDiffCardCount) do
with RestMengeRemSet[i] do
begin
ElemCount := 0;
Line 737:
end;
 
procedure Permute(depth,MaxCardsUsed:integerNativeInt);
var
pMengeElem : ^tMengenElemtSetElem;
i : NativeInt;
pMengeElem : ^tMengenElem;
begin
i := 0;
pMengeElem := @RestMengenRemainSets[depth].RestMengeRemSet[i];
repeat
if pMengeElem^.Elemcount <> 0 then begin
//take elementone of the same elements of the stack
dec(pMengeElem^.ElemCount);
//insert in result here string too
CardSequence[depth] := pMengeElem^.Elem;
CardString[depth+1] := chr(pMengeElem^.Elem+Ord('0'));
 
//done one permutation
IF depth = MaxCardsUsed then begin
inc(permCount);begin
inc(permCount);
//writeln(CardString[ depth+2] := #0);
// ###########
setlengthCheckPrime(CardString,CardSequence[depth+1]);
CardString[ depth+2] := #0;
CheckPrime(CardSequence[depth]);
// ###########
mindepth := depth;
end
else begin
begin
RestMengen[depth+1]:= RestMengen[depth];
Permute(RemainSets[depth+1,MaxCardsUsed)]:= RemainSets[depth];
//insert elementPermute(depth+1,MaxCardsUsed);
//re-insert that element
inc(pMengeElem^.ElemCount);
decinc(minDepthpMengeElem^.ElemCount);
enddec(minDepth);
end;
end;
//move on to the next digit
inc(pMengeElem);
inc(i);
until i >=RestMengenRemainSets[depth].MaxUsedIdx;
end;
 
Line 779 ⟶ 781:
i,dgtCnt,cnt,dgtIdx : NativeInt;
Begin
MengeInitSetInit(RestmengenRemainSets[0]);
dgtCnt := 0;
dgtIdx := 0;
//creating the start set.
with RestmengenRemainSets[0] do
Begin
For i in tDiffCardCount do
Line 789 ⟶ 792:
if cnt > 0 then
Begin
with RestMengeRemSet[dgtIdx] do
Begin
Elemcount := cnt;
Line 798 ⟶ 801:
end;
end;
TotElemCnt := dgtCnt;
MaxUsedIdx := dgtIdx;
end;
setlength(CardString,dgtCnt);
 
permute(0,dgtCnt-1);
end;
Line 817 ⟶ 821:
Begin
Check(n);
//n is 0 based count combinations of length n
inc(TotalUsedDigits[n+1]);
end;
end;
Line 823 ⟶ 828:
end;
 
procedure CheckAll(SumGoal:NativeInt);
var
i :NativeInt;
begin
BEGIN
IF sumGoal>cTotalSum then
EXIT;
permcount:=0;
fillchar(ManifoldOfDigit[0],SizeOf(ManifoldOfDigit),#0);
 
count := 0;
For i := 1 to 9 do
AppendToSum(0,i,cTotalSumSumGoal-i);
writeln('Count of generated numbers with digits sum of ',cTotalSumSumGoal,' are ',permcount);
Writelnwriteln('Propably primes ',count);
writeln;
//For i := 0 to cTotalSum-1 do writeln(i+1:3,TotalUsedDigits[i]:10); writeln;
end;
var
GoalSum: 0..cTotalSum;
BEGIN
CheckAll(25);
CheckAll(1*9);
CheckAll(2*9);
CheckAll(3*9);
END.</lang>
{{out}}
<pre>
Count of generated numbers with digits sum of 25 are 16499120
Propably primes 1525142 //only this real 0m6,880s
 
Count of generated numbers with digits sum of 9 are 256
Propably primes 0
 
Count of generated numbers with digits sum of 18 are 129792
Propably primes 0
 
Count of generated numbers with digits sum of 27 are 65866496
Propably primes 0
 
real 0m13,137s
 
// without testing for prime real 0m60m2,875s590s </pre>
 
=={{header|Perl}}==
Anonymous user