Numbers with prime digits whose sum is 13: Difference between revisions

→‎{{header|Phix}}: added Pascal only counting
m (→‎{{header|Go}}: added version for counting after sum 103 must use BIGINT)
(→‎{{header|Phix}}: added Pascal only counting)
Line 700:
</pre>
 
=={{header|Pascal}}==
{{Works with|Free Pascal}} Only counting.<BR>Extreme fast in finding the sum of primesdigits = value.<BR>Limited by Uint64
<lang pascal>program PrimSumUpTo13;
{$IFDEF FPC}
{$MODE DELPHI}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils;
 
type
tDigits = array[0..3] of Uint32;
const
MAXNUM = 113;
var
gblPrimDgtCnt :tDigits;
gblCount: NativeUint;
gblNom :array [0..MAXNUM Div 2] of NativeUInt;
 
function isPrime(n: NativeUint):boolean;
var
i : NativeUInt;
Begin
result := (n>1);
if n<4 then
EXIT;
result := false;
if n AND 1 = 0 then
EXIT;
i := 3;
while i*i<= n do
Begin
If n MOD i = 0 then
EXIT;
inc(i,2);
end;
result := true;
end;
 
procedure Sort(var t:tDigits);
// Sort descending to minimize * / in Calcpermcount
var
i,j,k: NativeUint;
temp : Uint32;
Begin
For k := 0 to high(tdigits)-1 do
Begin
temp:= t[k];
j := k;
For i := k+1 to high(tdigits) do
Begin
if temp < t[i] then
Begin
temp := t[i];
j := i;
end;
end;
t[j] := t[k];
t[k] := temp;
end;
end;
 
function CalcPermCount:NativeUint;
//permcount = n! /(TempDgtCnt[0]!*TempDgtCnt[1]!*TempDgtCnt[2]!*TempDgtCnt[3]!);
//TempDgtCnt[0] = 3 and TempDgtCnt[1..3]= 2 -dgtcnt = 3+3*2= 9
//nom = 1,2,3, 4,5,6,7,8,9
//denom = 1,2,3, 1,2,1,2,1,2
var
pgblNom : ^NativeUInt;
TempDgtCnt : tdigits;
i : NativeUint;
begin
TempDgtCnt := gblPrimDgtCnt;
Sort(TempDgtCnt);
pgblNom := @gblNom[1];
//jump over 1/1*2/2*3/3*4/4*..* TempDgtCnt[0]/TempDgtCnt[0]
inc(pgblNom,TempDgtCnt[0]);
result :=1;
 
For i := 1 to TempDgtCnt[1] do
Begin
result := (result*pgblNom^) DIV i;
inc(pgblNom);
end;
For i := 1 to TempDgtCnt[2] do
Begin
result := (result*pgblNom^) DIV i;
inc(pgblNom);
end;
For i := 1 to TempDgtCnt[3] do
Begin
result := (result*pgblNom^) DIV i;
inc(pgblNom);
end;
end;
 
procedure check32(sum3 :NativeUint);
var
n3 : nativeInt;
begin
n3 := sum3 DIV 3;
gblPrimDgtCnt[1]:= 0;
while n3 >= 0 do
begin
//divisible by 2
if sum3 AND 1 = 0 then
Begin
gblPrimDgtCnt[0] := sum3 shr 1;
inc(gblCount,CalcPermCount);
sum3 -= 3;
inc(gblPrimDgtCnt[1]);
dec(n3);
end;
sum3 -= 3;
inc(gblPrimDgtCnt[1]);
dec(n3);
end;
end;
 
var
Num : NativeUint;
i,sum7,sum5: NativeInt;
BEGIN
writeln('Sum':6,'Count of arrangements':25);
// array for n! 1,2,3,4,5,6..n
gblNom[0]:= 1;
For i := 1 to High(gblNom) do
gblNom[i] := i;
 
Num := 1;
repeat
inc(num);
if Not(isPrime(Num)) then
CONTINUE;
gblCount := 0;
sum7 :=num;
gblPrimDgtCnt[3] := 0;
while sum7 >=0 do
Begin
sum5 := sum7;
gblPrimDgtCnt[2]:=0;
while sum5 >= 0 do
Begin
check32(sum5);
dec(sum5,5);
inc(gblPrimDgtCnt[2]);
end;
inc(gblPrimDgtCnt[3]);
dec(sum7,7);
end;
writeln(num:6,gblCount:25,' ');
until num > MAXNUM;
END.</lang>
{{Out}}
<pre> Sum Count of arrangements
2 1
3 1
5 3
7 6
11 19
13 43
17 221
19 468
23 2098
29 21049
31 45148
37 446635
41 2061697
43 4427752
47 20424241
53 202405001
59 2005642061
61 4307930784
67 42688517778
71 196942068394
73 423011795680
79 4191737820642
83 19338456915087
89 191629965405641
97 4078672831913824
101 18816835854129198
103 40416663565084464
107 186461075642340151
109 400499564627237889
113 1847692833654336940
real 0m0,003s
</pre>
=={{header|Phix}}==
<lang Phix>function unlucky(sequence set, integer needed, string v="", sequence res={})
Anonymous user