Jump to content

Primes whose sum of digits is 25: Difference between revisions

→‎{{header|Pascal}}: reduced count of generated numbers for testing,move 1,3,7,9 and permute digits in front
(Realize in F#)
(→‎{{header|Pascal}}: reduced count of generated numbers for testing,move 1,3,7,9 and permute digits in front)
Line 1,055:
 
=={{header|Pascal}}==
added only strechted goal.Generating the combination of the digits for the numbers and afterwards generating the [[Permutations with some identical elements]]<BR>
Now seting one digit out of 1,3,7,9 to the end and permute the rest of the digits in front.<BR>So much less numbers have to be tested.10.5e6 instead of 16.4e6.Generating of the numbers is reduced in the same ratio.
<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>
Added sum to n*9 => no primes ;-)
<lang pascal>program Perm5aus8;
//formerly roborally take 5 cards out of 8
Line 1,071 ⟶ 1,069:
gmp;
const
cTotalSum = 6331;
 
cMaxCardsOnDeck = cTotalSum;//8
Line 1,083 ⟶ 1,081:
 
tSetElem = record
Elem : tDiffCardCount;
Elemcount : tDeckIndex;
Elem : tDiffCardCountend;
end;
 
tSet = record
RemSet : array [low(tDiffCardCount)..High(tDiffCardCount)] of tSetElem;
MaxUsedIdx,
TotElemCnt : wordbyte;
end;
 
tRemainSet = array [low(tSequenceIndex)..High(tSequenceIndex)+1] of tSet;
 
tCardSequence = array [low(tSequenceIndex)..High(tSequenceIndex)] of tDiffCardCount;
tPermRec = packed record
permTiefe : byte;// Stelle der Änderung gegenüber Vorgängerpermutation
permCardSequence :tCardSequence ;
end;
 
var
ManifoldOfDigit : array[tDiffCardCount] of Byte;
TotalUsedDigits : array[tDeckIndex] of Byte;
RemainSets : tRemainSet;
 
TotalUsedDigits : array[tDeckIndex] of Int32;
ManifoldOfDigit : array[tDiffCardCount] of Int32;
CardSequence : tCardSequence;
CardString : AnsiString;
 
PrimeCount : integer;
Count: NativeInt;
PermCount : integer;
mindepth : integer;
 
//*****************************************************************************
var
procedure SetInit(var ioMenge:tSet);
CS : pchar;
z : mpz_t;
 
procedure SetInit(var ioSet:tSet);
var
i : integer;
begin
with ioMengeioSet do
begin
MaxUsedIdx := 0;
Line 1,129 ⟶ 1,126:
end;
 
procedure CheckPrime(LastDigit:NativeInt);inline;
var
z : mpz_t;
begin
mpz_set_str(z,CS,10);
if Lastdigit in [1,3,7,9] then
inc(PrimeCount,ORD(mpz_probab_prime_p(z,3)>0));
Begin
mpz_init_set_str(z,pChar(@CardString[1]),10);
if mpz_probab_prime_p(z,1)>0 then
inc(count);
mpz_clear(z);
end;
end;
 
procedure Permute(depth,MaxCardsUsed:NativeInt);
var
pMengeElempSetElem : ^tSetElem;
i : NativeInt;
begin
i := 0;
pMengeElempSetElem := @RemainSets[depth].RemSet[i];
repeat
if pMengeElempSetElem^.Elemcount <> 0 then begin
//take one of the same elements of the stack
//insert in result here string
dec(pMengeElem^.ElemCount);
CS[depth] := chr(pSetElem^.Elem+Ord('0'));
//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);
//writeln(CardString)CheckPrime;
// ###########
CheckPrime(CardSequence[depth]);
// ###########
mindepth := depth;
end
else
begin
dec(pSetElem^.ElemCount);
RemainSets[depth+1]:= RemainSets[depth];
Permute(depth+1,MaxCardsUsed);
//re-insert that element
inc(pMengeElempSetElem^.ElemCount);
dec(minDepth);
end;
end;
//move on to the next digit
inc(pMengeElempSetElem);
inc(i);
until i >=RemainSets[depth].MaxUsedIdx;
Line 1,208 ⟶ 1,191:
TotElemCnt := dgtCnt;
MaxUsedIdx := dgtIdx;
 
CS := @CardString[1];
//Check only useful end-digits
For i := 0 to dgtIdx-1 do
Begin
if RemSet[i].Elem in[1,3,7,9]then
Begin
CS[dgtCnt-1] := chr(RemSet[i].Elem+Ord('0'));
CS[dgtCnt] := #00;
 
dec(RemSet[i].ElemCount);
permute(0,dgtCnt-2);
inc(RemSet[i].ElemCount);
end;
end;
end;
setlength(CardString,dgtCnt);
permute(0,dgtCnt-1);
end;
 
Line 1,226 ⟶ 1,222:
Begin
Check(n);
//n is 0 based countPrimeCount combinations of length n
inc(TotalUsedDigits[n+1]);
end;
Line 1,237 ⟶ 1,233:
i :NativeInt;
begin
setlength(CardString,SumGoal);
IF sumGoal>cTotalSum then
EXIT;
permcount:=0;
fillchar(ManifoldOfDigit[0],SizeOf(ManifoldOfDigit),#0);
count permcount:= 0;
PrimeCount := 0;
 
For i := 1 to 9 do
AppendToSum(0,i,SumGoal-i);
 
writeln('Count of generated numbers with digits sum of ',SumGoal,' are ',permcount);
writeln('PrimeCount of generated numbers with digits sum of ',SumGoal,' are ',permcount);
writeln('Propably primes ',count);
writeln('Propably primes ',PrimeCount);
writeln;
end;
var
GoalSumT1,T0 : 0..cTotalSumInt64;
SumGoal: NativeInt;
BEGIN
writeln('GMP-Version ',gmp.version);
CheckAll(25);
CheckAllmpz_init_set_ui(1*9z,0);
T0 := GetTickCount64;
CheckAll(2*9);
For SumGoal := 25 to 25 do
CheckAll(3*9);
Begin
END.</lang>
CheckAll(SumGoal);
T1 := GetTickCount64;Writeln((T1-T0)/1000:7:3,' s');
T0 := T1;
end;
mpz_clear(z);
END.
</lang>
{{out}}
<pre>
//Runnning on TIO.RUN
Count of generated numbers with digits sum of 25 are 16499120
GMP-Version 6.1.2
Propably primes 1525142 //only this real 0m6,880s
PrimeCount of generated numbers with digits sum of 25 are 10488498
 
Propably primes 1525141
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
 
9.932 s
real 0m13,137s
....
Free Pascal Compiler version 3.0.4 [2018/07/13] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling .code.tio.pp
Linking .bin.tio
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
204 lines compiled, 0.2 sec
 
Real time: 10.135 s
// without testing for prime real 0m2,590s </pre>
User time: 10.027 s
Sys. time: 0.052 s
CPU share: 99.45 %
Exit code: 0
</pre>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.