Anonymous user
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
<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
<lang pascal>▼
Added sum to n*9 => no primes ;-)
▲<lang pascal>program Perm5aus8;
//formerly roborally take 5 cards out of 8
{$IFDEF FPC}
{$mode Delphi}
Line 664 ⟶ 666:
gmp;
const
cTotalSum =
cMaxCardsOnDeck = cTotalSum;//8
CMaxCardsUsed = cTotalSum;//5
type
Line 675 ⟶ 677:
tDiffCardCount = 0..9;
Elemcount : tDeckIndex;
Elem : tDiffCardCount;
end;
MaxUsedIdx,
TotElemCnt : word;
end;
tCardSequence = array [low(tSequenceIndex)..High(tSequenceIndex)] of tDiffCardCount;
Line 695 ⟶ 697:
var
RemainSets : tRemainSet;
TotalUsedDigits : array[tDeckIndex] of Int32;
ManifoldOfDigit : array[tDiffCardCount] of Int32;
CardSequence : tCardSequence;
CardString :
Count: NativeInt;
Line 708:
//*****************************************************************************
procedure
var
i : integer;
Line 716:
MaxUsedIdx := 0;
For i := Low(tDiffCardCount) to High(tDiffCardCount) do
with
begin
ElemCount := 0;
Line 737:
end;
procedure Permute(depth,MaxCardsUsed:
var
i : NativeInt;
▲ pMengeElem : ^tMengenElem;
begin
i := 0;
pMengeElem := @
repeat
if
inc(permCount);
// ###########
▲ CardString[ depth+2] := #0;
// ###########
begin
//re-insert that element
end;
//move on to the next digit
inc(pMengeElem);
inc(i);
until i >=
end;
Line 779 ⟶ 781:
i,dgtCnt,cnt,dgtIdx : NativeInt;
Begin
dgtCnt := 0;
dgtIdx := 0;
//creating the start set.
with
Begin
For i in tDiffCardCount do
Line 789 ⟶ 792:
if cnt > 0 then
Begin
with
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,
writeln('Count of generated numbers with digits sum of ',
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
=={{header|Perl}}==
|