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

Added Easylang
m (→‎{{header|Pascal}}: unnessasary gblnominator...)
(Added Easylang)
 
(47 intermediate revisions by 22 users not shown)
Line 1:
{{draft task|Prime Numbers}}
Find all the decimalpositive numbersintegers whose decimal digits are all primes and sum to   '''13'''.
<br><br>
 
=={{header|11l}}==
{{trans|C}}
<syntaxhighlight lang="11l">F primeDigitsSum13(=n)
V sum = 0
L n > 0
V r = n % 10
I r !C (2, 3, 5, 7)
R 0B
n I/= 10
sum += r
R sum == 13
 
V c = 0
L(i) 1..999999
I primeDigitsSum13(i)
print(‘#6’.format(i), end' ‘ ’)
I ++c == 11
c = 0
print()
print()</syntaxhighlight>
 
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE PRIMECOUNT="4"
BYTE ARRAY primedigits(PRIMECOUNT)=[2 3 5 7]
 
PROC PrintNum(BYTE ARRAY code BYTE count)
BYTE i,c
 
FOR i=0 TO count-1
DO
c=code(i)
Put(primedigits(c)+'0)
OD
RETURN
 
BYTE FUNC Sum(BYTE ARRAY code BYTE count)
BYTE i,res,c
 
res=0
FOR i=0 TO count-1
DO
c=code(i)
res==+primedigits(c)
OD
RETURN (res)
 
PROC Init(BYTE ARRAY code BYTE count)
Zero(code,count)
RETURN
 
BYTE FUNC Next(BYTE ARRAY code BYTE count)
INT pos,c
 
pos=count-1
DO
c=code(pos)+1
IF c<PRIMECOUNT THEN
code(pos)=c
RETURN (1)
FI
code(pos)=0
pos==-1
IF pos<0 THEN
RETURN (0)
FI
OD
RETURN (0)
 
PROC Main()
DEFINE MAXDIG="6"
BYTE ARRAY digits(MAXDIG)
BYTE count,pos
 
count=1 pos=0
Init(count)
DO
IF Sum(digits,count)=13 THEN
IF pos+count>=38 THEN
pos=0 PutE()
FI
PrintNum(digits,count) Put(32)
pos==+count+1
FI
IF Next(digits,count)=0 THEN
count==+1
IF count>MAXDIG THEN
EXIT
FI
Init(count)
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Numbers_with_prime_digits_whose_sum_is_13.png Screenshot from Atari 8-bit computer]
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232
222322 223222 232222 322222
</pre>
 
=={{header|ALGOL 68}}==
Based on the Algol W sample.
<syntaxhighlight lang="algol68">BEGIN
# find numbers whose digits are prime and whose digit sum is 13 #
# as noted by the Wren sample, the digits can only be 2, 3, 5, 7 #
# and there can only be 3, 4, 5 or 6 digits #
[]INT possible digits = []INT( 0, 2, 3, 5, 7 )[ AT 0 ];
INT number count := 0;
INT zero = ABS "0"; # integer value of the character "0" #
print( ( newline ) );
FOR d1 index FROM 0 TO UPB possible digits DO
INT d1 = possible digits[ d1 index ];
CHAR c1 = IF d1 /= 0 THEN REPR ( d1 + zero ) ELSE " " FI;
FOR d2 index FROM 0 TO UPB possible digits DO
INT d2 = possible digits[ d2 index ];
IF d2 /= 0 OR d1 = 0 THEN
CHAR c2 = IF ( d1 + d2 ) /= 0 THEN REPR ( d2 + zero ) ELSE " " FI;
FOR d3 index FROM 0 TO UPB possible digits DO
INT d3 = possible digits[ d3 index ];
IF d3 /= 0 OR ( d1 + d2 ) = 0 THEN
CHAR c3 = IF ( d1 + d2 + d3 ) /= 0 THEN REPR ( d3 + zero ) ELSE " " FI;
FOR d4 index FROM 1 TO UPB possible digits DO
INT d4 = possible digits[ d4 index ];
CHAR c4 = REPR ( d4 + zero );
FOR d5 index FROM 1 TO UPB possible digits DO
INT d5 = possible digits[ d5 index ];
CHAR c5 = REPR ( d5 + zero );
FOR d6 index FROM 1 TO UPB possible digits DO
INT d6 = possible digits[ d6 index ];
IF ( d1 + d2 + d3 + d4 + d5 + d6 ) = 13 THEN
# found a number whose prime digits sum to 13 #
CHAR c6 = REPR ( d6 + zero );
print( ( " ", c1, c2, c3, c4, c5, c6 ) );
number count := number count + 1;
IF ( number count +:= 1 ) MOD 12 = 0 THEN print( ( newline ) ) FI
FI
OD # d6 #
OD # d5 #
OD # d4 #
FI
OD # d3 #
FI
OD # d2 #
OD # d1 #
END</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722
3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252
22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322
52222 222223 222232 222322 223222 232222 322222
</pre>
 
<br>
=={{header|ALGOL W}}==
Uses the observations about the digits and numbers in the Wren solution to generate the sequence.
<langsyntaxhighlight lang="algolw">begin
% find numbers whose digits are prime and whose digit sum is 13 %
% as noted by the Wren sample, the digits can only be 2, 3, 5, 7 %
Line 39 ⟶ 200:
end for_d2
end for_d1
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 47 ⟶ 208:
52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pDigits: [2 3 5 7]
 
lst: map pDigits 'd -> @[d]
result: new []
 
while [0 <> size lst][
nextList: new []
loop lst 'digitSeq [
currSum: sum digitSeq
loop pDigits 'n [
newSum: currSum + n
newDigitSeq: digitSeq ++ n
case [newSum]
when? [<13] -> 'nextList ++ @[newDigitSeq]
when? [=13] -> 'result ++ @[to :integer join to [:string] newDigitSeq]
else -> break
]
]
lst: new nextList
]
 
loop split.every: 10 result 'a ->
print map a => [pad to :string & 6]</syntaxhighlight>
 
{{out}}
 
<pre> 337 355 373 535 553 733 2227 2272 2335 2353
2533 2722 3235 3253 3325 3352 3523 3532 5233 5323
5332 7222 22225 22252 22333 22522 23233 23323 23332 25222
32233 32323 32332 33223 33232 33322 52222 222223 222232 222322
223222 232222 322222</pre>
 
=={{header|AWK}}==
===Counting and testing===
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WITH_PRIME_DIGITS_WHOSE_SUM_IS_13.AWK
BEGIN {
for (i=1; i<=1000000; i++) {
if (prime_digits_sum13(i)) {
printf("%6d ",i)
if (++count % 10 == 0) {
printf("\n")
}
}
}
printf("\n")
exit(0)
}
function prime_digits_sum13(n, r,sum) {
while (n > 0) {
r = int(n % 10)
switch (r) {
case 2:
case 3:
case 5:
case 7:
break
default:
return(0)
}
n = int(n / 10)
sum += r
}
return(sum == 13)
}
</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353
2533 2722 3235 3253 3325 3352 3523 3532 5233 5323
5332 7222 22225 22252 22333 22522 23233 23323 23332 25222
32233 32323 32332 33223 33232 33322 52222 222223 222232 222322
223222 232222 322222
</pre>
===Generate digit combinations directly===
<syntaxhighlight lang="awk">BEGIN {
o = 1
src[o++] = 13
do {
r = src[++i]
n = src[++i]
for (p = 2; p != 9; p += p % 2 + 1) {
if (p >= r) {
if (p == r) res = res " " n p
break
}
src[++o] = r - p
src[++o] = n p
}
} while (i != o)
print substr(res, 2)
}</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|bc}}==
<syntaxhighlight lang="bc">q[0] = 13
o = 2
while (i != o) {
r = q[i++]
n = q[i++]
for (p = 2; p != 9; p += p % 2 + 1) {
if (p >= r) {
if (p == r) n + p
break
}
q[o++] = r - p
q[o++] = (n + p) * 10
}
}</syntaxhighlight>
{{out}}
<pre style="max-height:12em">
337
355
373
535
553
733
2227
2272
2335
2353
2533
2722
3235
3253
3325
3352
3523
3532
5233
5323
5332
7222
22225
22252
22333
22522
23233
23323
23332
25222
32233
32323
32332
33223
33232
33322
52222
222223
222232
222322
223222
232222
322222
</pre>
 
=={{header|C}}==
Brute force
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
bool primeDigitsSum13(int n) {
int sum = 0;
while (n > 0) {
int r = n % 10;
switch (r) {
case 2:
case 3:
case 5:
case 7:
break;
default:
return false;
}
n /= 10;
sum += r;
}
return sum == 13;
}
 
int main() {
int i, c;
 
// using 2 for all digits, 6 digits is the max prior to over-shooting 13
c = 0;
for (i = 1; i < 1000000; i++) {
if (primeDigitsSum13(i)) {
printf("%6d ", i);
if (c++ == 10) {
c = 0;
printf("\n");
}
}
}
printf("\n");
 
return 0;
}</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|C#|CSharp}}==
{{Trans|Phix}}
Same recursive method.
<langsyntaxhighlight lang="csharp">using System;
using static System.Console;
using LI = System.Collections.Generic.SortedSet<int>;
Line 65 ⟶ 432:
static void Main(string[] args) { WriteLine(string.Join(" ",
unl(new LI {}, new LI { 2, 3, 5, 7 }, 13))); }
}</langsyntaxhighlight>
{{out}}
<pre style="white-space:pre-wrap;">337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
Line 71 ⟶ 438:
=== Alternate ===
Based in '''Nigel Galloway's''' suggestion from the discussion page.
<langsyntaxhighlight lang="csharp">class Program {
 
static void Main(string[] args) { int[] lst; int sum;
Line 81 ⟶ 448:
else if (sum < 12)
w.Add((i.digs * 10 + j, sum)); } }
}</langsyntaxhighlight>
Same output.
 
=={{header|C++}}==
{{trans|C#}}(the alternate version)
<langsyntaxhighlight lang="cpp">#include <cstdio>
#include <vector>
#include <bits/stdc++.h>
Line 99 ⟶ 466:
printf("%d%d ", get<0>(i), x);
else if (sum < 12) w.push_back({get<0>(i) * 10 + x, sum}); }
return 0; }</langsyntaxhighlight>
Same output as C#.
 
=={{header|D}}==
{{trans|C}}
<syntaxhighlight lang="d">import std.stdio;
 
bool primeDigitsSum13(int n) {
int sum = 0;
while (n > 0) {
int r = n % 10;
switch (r) {
case 2,3,5,7:
break;
default:
return false;
}
n /= 10;
sum += r;
}
return sum == 13;
}
 
void main() {
// using 2 for all digits, 6 digits is the max prior to over-shooting 13
int c = 0;
for (int i = 1; i < 1_000_000; i++) {
if (primeDigitsSum13(i)) {
writef("%6d ", i);
if (c++ == 10) {
c = 0;
writeln;
}
}
}
writeln;
}</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func digprimsum13 n .
while n > 0
d = n mod 10
if d < 2 or d = 4 or d = 6 or d >= 8
return 0
.
sum += d
n = n div 10
.
return if sum = 13
.
p = 2
while p <= 322222
if digprimsum13 p = 1
write p & " "
.
p += 1
.
</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// prime digits whose sum is 13. Nigel Galloway: October 21st., 2020
let rec fN g=let g=[for n in [2;3;5;7] do for g in g->n::g]|>List.groupBy(fun n->match List.sum n with 13->'n' |n when n<12->'g' |_->'x')|>Map.ofSeq
[yield! (if g.ContainsKey 'n' then g.['n'] else []); yield! (if g.ContainsKey 'g' then fN g.['g'] else [])]
fN [[]] |> Seq.iter(fun n->n|>List.iter(printf "%d");printf " ");printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|Factor}}==
===Filtering selections===
Generate all selections of the prime digits in the only possible lengths whose sum can be 13, then filter for sums that equal 13.
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.combinatorics
math.functions math.ranges sequences sequences.extras ;
 
Line 123 ⟶ 557:
"Numbers whose digits are prime and sum to 13:" print
{ 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with
map-concat [ digits>number ] map "%[%d, %]\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 129 ⟶ 563:
{ 337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222 }
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
 
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
var T: integer;
begin
SetLength(IA,0);
repeat
begin
T:=N mod 10;
N:=N div 10;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=T;
end
until N<1;
end;
 
 
function IsPrimeDigitSum13(N: integer): boolean;
{Return true N's digits are prime and total 13}
var IA: TIntegerDynArray;
var I,Sum: integer;
begin
Result:=False;
GetDigits(N,IA);
for I:=0 to High(IA) do
if not IsPrime(IA[I]) then exit;
Sum:=0;
for I:=0 to High(IA) do Sum:=Sum+IA[I];
Result:=Sum=13;
end;
 
procedure ShowPrimeDigitSum13(Memo: TMemo);
{Show numbers whose digits are prime and total 13}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=1 to 999999 do
if IsPrimeDigitSum13(I) then
begin
Inc(Cnt);
S:=S+Format('%8D',[I]);
If (Cnt mod 5)=0 then S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553
733 2227 2272 2335 2353
2533 2722 3235 3253 3325
3352 3523 3532 5233 5323
5332 7222 22225 22252 22333
22522 23233 23323 23332 25222
32233 32323 32332 33223 33232
33322 52222 222223 222232 222322
223222 232222 322222
Elapsed Time: 627.207 ms.
 
</pre>
 
 
===F# translation===
The following is based on Nigel Galloway's algorithm as described [http://rosettacode.org/wiki/Talk:Numbers_with_prime_digits_whose_sum_is_13#Nice_recursive_solution here] on the talk page. It's about 10x faster than the previous method.
<langsyntaxhighlight lang="factor">USING: io kernel math prettyprint sequences sequences.extras ;
 
{ } { { 2 } { 3 } { 5 } { 7 } } [
{ 2 3 5 7 } [ suffix ] cartesian-map concat
[ sum 13 = ] partition [ append ] dip [ sum 11 > ] reject
] until-empty [ bl ] [ [ pprint ] each ] interleave nl</langsyntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|FreeBASIC}}==
Ho hum. Another prime digits task.
 
<syntaxhighlight lang="freebasic">
function digit_is_prime( n as integer ) as boolean
select case n
case 2,3,5,7
return true
case else
return false
end select
end function
 
function all_digits_prime( n as uinteger ) as boolean
dim as string sn = str(n)
for i as uinteger = 1 to len(sn)
if not digit_is_prime( val(mid(sn,i,1)) ) then return false
next i
return true
end function
 
function digit_sum_13( n as uinteger ) as boolean
dim as string sn = str(n)
dim as integer k = 0
for i as uinteger = 1 to len(sn)
k = k + val(mid(sn,i,1))
if k>13 then return false
next i
if k<>13 then return false else return true
end function
 
for i as uinteger = 1 to 322222
if all_digits_prime(i) andalso digit_sum_13(i) then print i,
next i</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733
2227 2272 2335 2353 2533 2722
3235 3253 3325 3352 3523 3532
5233 5323 5332 7222 22225 22252
22333 22522 23233 23323 23332 25222
32233 32323 32332 33223 33232 33322
52222 222223 222232 222322 223222 232222
322222</pre>
 
 
=={{header|Go}}==
Reuses code from some other tasks.
<langsyntaxhighlight lang="go">package main
 
import (
Line 217 ⟶ 791:
fmt.Println("Those numbers whose digits are all prime and sum to 13 are:")
fmt.Println(res2)
}</langsyntaxhighlight>
 
{{out}}
Line 226 ⟶ 800:
===only counting===
See Julia [http://rosettacode.org/wiki/Numbers_with_prime_digits_whose_sum_is_13#Julia]
<syntaxhighlight lang="go">
<lang go>
package main
 
Line 315 ⟶ 889:
fmt.Println("The count of numbers whose digits are all prime and sum to",mySum,"is",gblCount)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 352 ⟶ 926:
=={{header|Haskell}}==
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
import Data.List (intercalate, transpose, unfoldr)
import Text.Printf
Line 401 ⟶ 975:
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0</langsyntaxhighlight>
{{Out}}
<pre>43 numbers with prime digits summing to 13:
Line 410 ⟶ 984:
32233 32323 32332 33223 33232 33322 52222 222223 222232 222322
223222 232222 322222</pre>
 
=={{header|J}}==
 
Galloway's algorithm, from the talk page:
 
<syntaxhighlight lang=J>ps13=: {{
seq=. 0#,D=. ,Q=. ":,.p:i.4
while. #Q do.
N=. ,/D,"0 1/Q
i=. +/"1"."0 N
Q=. (12>i)#N
seq=. seq,,".(13=i)#N
end.
}}0</syntaxhighlight>
 
Here, upper case names are character arrays representing digits, lower case names are numeric arrays.
 
<code>Q</code> (number suffixes) and <code>N</code> (candidate numbers) represent sequences of sequences of characters. (The top level sequence -- rows -- distinguishes different potential numbers and the secondary sequence -- columns -- distinguishes digits within potential numbers).
 
Meanwhile, <code>D</code> (prime digits), <code>i</code> (digit sums) and <code>seq</code> (numbers whose digit sum is 13) are simple sequences.
 
<syntaxhighlight lang=J> ps13
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang="java">public class PrimeDigits {
private static boolean primeDigitsSum13(int n) {
int sum = 0;
while (n > 0) {
int r = n % 10;
if (r != 2 && r != 3 && r != 5 && r != 7) {
return false;
}
n /= 10;
sum += r;
}
return sum == 13;
}
 
public static void main(String[] args) {
// using 2 for all digits, 6 digits is the max prior to over-shooting 13
int c = 0;
for (int i = 1; i < 1_000_000; i++) {
if (primeDigitsSum13(i)) {
System.out.printf("%6d ", i);
if (c++ == 10) {
c = 0;
System.out.println();
}
}
}
System.out.println();
}
}</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 </pre>
 
=={{header|JavaScript}}==
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 593 ⟶ 1,227:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>337,355,373,535,553,733
Line 603 ⟶ 1,237:
52222,222223,222232,222322,223222,232222
322222</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The first two solutions presented in this section focus on the specific task posed in the title of this page, and the third is a fast analytic solution for determining the count of distinct numbers with prime digits whose sum is any given number. This third solution requires gojq for accuracy if the target sum is relatively large.
 
All three solutions are based on the observation that the only decimal digits which are prime are [2, 3, 5, 7].
 
To save space, the first two solutions only present the count of the number of solutions; this is done using the stream counter:
 
def count(s): reduce s as $_ (0; .+1);
 
====Simple Generate-and-Test Solution====
<syntaxhighlight lang="jq"># Output: a stream
def simple:
range(2; 7) as $n
| [2, 3, 5, 7]
| combinations($n)
| select(add == 13)
| join("") | tonumber;
 
count(simple)</syntaxhighlight>
{{out}}
<pre>
43
</pre>
====A Faster Solution====
<syntaxhighlight lang="jq">def faster:
def digits: [2, 3, 5, 7];
def wide($max):
def d: digits[] | select(. <= $max);
if . == 1 then d | [.]
else d as $first
| (. - 1 | wide($max - $first)) as $next
| [$first] + $next
end;
range(2; 7)
| wide(13)
| select(add == 13)
| join("") | tonumber;
 
count(faster)
</syntaxhighlight>
{{out}}
<pre>
43
</pre>
====Fast Computation of the Count of Numbers====
As indicated above, this third solution is analytical (combinatoric),
and requires gojq for accuracy for relatively large target sums.
<syntaxhighlight lang="jq"># Input should be a sorted array of distinct positive integers
# Output is a stream of distinct arrays, each of which is sorted, and each sum of which is $sum
def sorted_combinations($sum):
if $sum <= 0 or length == 0 or $sum < .[0] then empty
else range(0; length ) as $i
| .[$i] as $x
| (($sum / $x) | floor) as $maxn
| range(1; 1 + $maxn) as $n
| ([range(0; $n) | $x]) as $prefix
| ($prefix | add // 0 ) as $psum
| if $psum == $sum then $prefix
else $prefix + (.[$i+1 :] | sorted_combinations($sum - $psum) )
end
end;
 
def factorial: reduce range(2;.+1) as $i (1; . * $i);
 
def product_of_factorials:
reduce .[] as $n (1; . * ($n|factorial));
 
# count the number of distinct permutations
def count_distinct_permutations:
def histogram:
reduce .[] as $i ([]; .[$i] += 1);
(length|factorial) / (histogram|product_of_factorials);
 
def number_of_interesting_numbers($total):
def digits: [2, 3, 5, 7];
reduce (digits | sorted_combinations($total)) as $pattern (0;
. + ($pattern|count_distinct_permutations));
 
number_of_interesting_numbers(13),
number_of_interesting_numbers(199)</syntaxhighlight>
{{out}}
<pre>
43
349321957098598244959032342621956
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics, Primes
 
function primedigitsums(targetsum)
Line 652 ⟶ 1,375:
 
foreach(countprimedigitsums, nextprimes(17, 40))
</langsyntaxhighlight>{{out}}
<pre>
There are 3 prime-digit-only numbers summing to 5 : [5, 32, 23]
Line 699 ⟶ 1,422:
There are 349321957098598244959032342621956 prime-digit-only numbers summing to 199.
</pre>
 
=={{header|Kotlin}}==
{{trans|D}}
<syntaxhighlight lang="scala">fun primeDigitsSum13(n: Int): Boolean {
var nn = n
var sum = 0
while (nn > 0) {
val r = nn % 10
if (r != 2 && r != 3 && r != 5 && r != 7) {
return false
}
nn /= 10
sum += r
}
return sum == 13
}
 
fun main() {
// using 2 for all digits, 6 digits is the max prior to over-shooting 13
var c = 0
for (i in 1 until 1000000) {
if (primeDigitsSum13(i)) {
print("%6d ".format(i))
if (c++ == 10) {
c = 0
println()
}
}
}
println()
}</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 </pre>
 
=={{header|Lua}}==
{{trans|C}}
<syntaxhighlight lang="lua">function prime_digits_sum_13(n)
local sum = 0
while n > 0 do
local r = n % 10
if r ~= 2 and r ~= 3 and r ~= 5 and r ~= 7 then
return false
end
n = math.floor(n / 10)
sum = sum + r
end
return sum == 13
end
 
local c = 0
for i=1,999999 do
if prime_digits_sum_13(i) then
io.write(string.format("%6d ", i))
if c == 10 then
c = 0
print()
else
c = c + 1
end
end
end
print()</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 </pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, sequtils, strutils
 
type Digit = 0..9
 
proc toInt(s: seq[Digit]): int =
## Convert a sequence of digits to an integer.
for n in s:
result = 10 * result + n
 
const PrimeDigits = @[Digit 2, 3, 5, 7]
 
var list = PrimeDigits.mapIt(@[it]) # List of sequences of digits.
var result: seq[int]
while list.len != 0:
var nextList: seq[seq[Digit]] # List with one more digit.
for digitSeq in list:
let currSum = sum(digitSeq)
for n in PrimeDigits:
let newSum = currSum + n
let newDigitSeq = digitSeq & n
if newSum < 13: nextList.add newDigitSeq
elif newSum == 13: result.add newDigitSeq.toInt
else: break
list = move(nextList)
 
for i, n in result:
stdout.write ($n).align(6), if (i + 1) mod 9 == 0: '\n' else: ' '
echo()</syntaxhighlight>
 
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335
2353 2533 2722 3235 3253 3325 3352 3523 3532
5233 5323 5332 7222 22225 22252 22333 22522 23233
23323 23332 25222 32233 32323 32332 33223 33232 33322
52222 222223 222232 222322 223222 232222 322222 </pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let prime_digits_13 =
let digits = [2; 3; 5; 7] in
let rec next ds ns = function
| [] -> if ns = [] then [] else next digits [] (List.rev ns)
| (n, r) :: cs' as cs ->
match ds with
| d :: ds' when d < r -> next ds' (((n + d) * 10, r - d) :: ns) cs
| d :: ds' when d = r -> n + d :: next digits ns cs'
| _ -> next digits ns cs'
in next digits [] [0, 13]
 
let () =
List.map string_of_int prime_digits_13 |> String.concat " " |> print_endline</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|Pascal}}==
{{Works with|Free Pascal}} Only counting.<BR>Extreme fast in finding the sum of primesdigits = value.<BR>Limited by Uint64
<langsyntaxhighlight lang="pascal">program PrimSumUpTo13;
{$IFDEF FPC}
{$MODE DELPHI}
Line 764 ⟶ 1,611:
//TempDgtCnt[0] = 3 and TempDgtCnt[1..3]= 2 -> dgtcnt = 3+3*2= 9
//permcount = dgtcnt! /(TempDgtCnt[0]!*TempDgtCnt[1]!*TempDgtCnt[2]!*TempDgtCnt[3]!);
//nom of n! = 1,2,3, 4,5, 6,7, 8,9
//TempDgtCnt[0] = 3 and TempDgtCnt[1..3]= 2 -dgtcnt = 3+3*2= 9
//nomdenom = 1,2,3, 41,52,6 1,72,8 1,92
//denom = 1,2,3, 1,2,1,2,1,2
var
TempDgtCnt : tdigits;
Line 846 ⟶ 1,692:
writeln(num:6,gblCount:25,' ');
until num > MAXNUM;
END.</langsyntaxhighlight>
{{Out}}
<pre> Sum Count of arrangements
Line 880 ⟶ 1,726:
113 1847692833654336940
real 0m0,003s
</pre>
===using gmp===
<syntaxhighlight lang="pascal">program PrimSumUpTo13_GMP;
{$IFDEF FPC}
{$OPTIMIZATION ON,ALL}
{$MODE DELPHI}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
uses
sysutils,gmp;
 
type
tDigits = array[0..3] of Uint32;
const
MAXNUM = 199;//999
var
//split factors of n! in Uint64 groups
Fakul : array[0..MAXNUM] of UInt64;
IdxLimits : array[0..MAXNUM DIV 3] of word;
gblPrimDgtCnt :tDigits;
 
gblSum,
gbldelta : MPInteger; // multi precision (big) integers selve cleaning
s : AnsiString;
 
procedure Init;
var
i,j,tmp,n :NativeUint;
Begin
//generate n! by Uint64 factors
j := 1;
n := 0;
For i := 1 to MAXNUM do
begin
tmp := j;
j *= i;
if j div i <> tmp then
Begin
IdxLimits[n]:= i-1;
j := i;
inc(n);
end;
Fakul[i] := j;
end;
 
setlength(s,512);// 997 -> 166
z_init_set_ui(gblSum,0);
z_init_set_ui(gbldelta,0);
end;
 
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);
// sorting descending to reduce calculations
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 calcOne(f,n: NativeUint):NativeUint;
var
i,idx,MaxMulLmt : NativeUint;
Begin
result := f;
if n = 0 then
EXIT;
 
MaxMulLmt := High(MaxMulLmt) DIV (f+n);
z_mul_ui(gbldelta,gbldelta,result);
inc(result);
if n > 1 then
Begin
//multiply by parts of (f+n)!/f! with max Uint64 factors
i := 2;
while (i<=n) do
begin
idx := 1;
while (i<=n) AND (idx<MaxMulLmt) do
Begin
idx *= result;
inc(i);
inc(result);
end;
z_mul_ui(gbldelta,gbldelta,idx);
end;
 
//divide by n! with max Uint64 divisors
idx := 0;
if n > IdxLimits[idx] then
repeat
z_divexact_ui(gbldelta,gbldelta,Fakul[IdxLimits[idx]]);
inc(idx);
until IdxLimits[idx] >= n;
z_divexact_ui(gbldelta,gbldelta,Fakul[n]);
end;
end;
 
procedure CalcPermCount;
//TempDgtCnt[0] = 3 and TempDgtCnt[1..3]= 2 -> dgtcnt = 3+3*2= 9
//permcount = dgtcnt! /(TempDgtCnt[0]!*TempDgtCnt[1]!*TempDgtCnt[2]!*TempDgtCnt[3]!);
//nom of n! = 1,2,3, 4,5, 6,7, 8,9
//denom = 1,2,3, 1,2, 1,2, 1,2
var
TempDgtCnt : tdigits;
f : NativeUint;
begin
TempDgtCnt := gblPrimDgtCnt;
Sort(TempDgtCnt);
//jump over 1/1*2/2*3/3*4/4*..*
//res := 1;
f := TempDgtCnt[0]+1;
z_set_ui(gbldelta,1);
 
f := calcOne(f,TempDgtCnt[1]);
f := calcOne(f,TempDgtCnt[2]);
f := calcOne(f,TempDgtCnt[3]);
 
z_add(gblSum,gblSum,gblDelta);
end;
 
procedure check32(sum3 :NativeInt);
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;
CalcPermCount;
end;
sum3 -= 3;
inc(gblPrimDgtCnt[1]);
dec(n3);
end;
end;
procedure CheckAll(num:NativeUint);
var
sum7,sum5: NativeInt;
BEGIN
z_set_ui(gblSum,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;
end;
 
var
T0 : Int64;
Num : NativeUint;
BEGIN
Init;
 
T0 := GettickCount64;
// Number crunching goes here
writeln('Sum':6,'Count of arrangements':25);
For num := 2 to MAXNUM do
IF isPrime(Num) then
Begin
CheckAll(num);
 
z_get_str(pChar(s),10,gblSum);
writeln(num:6,' ',pChar(s));
end;
writeln('Time taken : ',GettickCount64-T0,' ms');
 
z_clear(gblSum);
z_clear(gbldelta);
END.</syntaxhighlight>
{{out}}
<pre>
tio.run/#pascal-fpc
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
127 389696778451488128521
131 1797854500757846669066
137 17815422682488317051838
139 38265729200380568226735
149 1749360471151229472803187
151 3757449669085729778349997
157 37233577041224219717325533
163 368957506121989278337474430
167 1702174484494837917764813972
173 16867303726643249517987636148
179 167142638782573042636172836062
181 359005512666242240589945886415
191 16412337250779890525195727788488
193 35252043354611887665339338710961
197 162634253887997896351270835136345
199 349321957098598244959032342621956
Time taken : 23 ms</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
use warnings;
 
my @queue = my @primedigits = ( 2, 3, 5, 7 );
my $numbers;
 
while( my $n = shift @queue )
{
if( eval $n == 13 )
{
$numbers .= $n =~ tr/+//dr . " ";
}
elsif( eval $n < 13 )
{
push @queue, map "$n+$_", @primedigits;
}
}
print $numbers =~ s/.{1,80}\K /\n/gr;</syntaxhighlight>
{{out}}
<pre>
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523
3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233
32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|Phix}}==
<!--(phixonline)-->
<lang Phix>function unlucky(sequence set, integer needed, string v="", sequence res={})
<syntaxhighlight lang="phix">
with javascript_semantics
function unlucky(sequence set, integer needed, string v="", sequence res={})
if needed=0 then
res = append(res,sprintf("%6s",v))
Line 893 ⟶ 2,038:
return res
end function
 
sequence r = sort(unlucky({2,3,5,7},13))
puts(1,join_by(r,1,11," "))</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 905 ⟶ 2,050:
===iterative===
Queue-based version of Nigel's recursive algorithm, same output.
<!--(phixonline)-->
<lang Phix>requires("0.8.2") -- uses latest apply() mods, rest is fine
<syntaxhighlight lang="phix">
with javascript_semantics
requires("0.8.2") -- uses latest apply() mods, rest is fine
constant dgts = {2,3,5,7}
function unlucky()
Line 921 ⟶ 2,069:
return res
end function
sequence r = unlucky()
r = apply(true,sprintf,{{"%6d"},r})
puts(1,join_by(r,1,11," "))</lang>
</syntaxhighlight>
 
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]].
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
digit_sum(N, M) :- digit_sum(N, 0, M).
digit_sum(0, A, B) :- !, A = B.
digit_sum(N, A0, M) :-
divmod(N, 10, Q, R),
plus(A0, R, A1),
digit_sum(Q, A1, M).
 
prime_digits(0).
prime_digits(N) :-
prime_digits(M),
member(D, [2, 3, 5, 7]),
N is 10 * M + D.
 
prime13(N) :-
prime_digits(N),
(N > 333_333 -> !, false ; true),
digit_sum(N, 13).
 
main :-
findall(N, prime13(N), S),
format("Those numbers whose digits are all prime and sum to 13 are: ~n~w~n", [S]),
halt.
 
?- main.
</syntaxhighlight>
{{Out}}
<pre>
Those numbers whose digits are all prime and sum to 13 are:
[337,355,373,535,553,733,2227,2272,2335,2353,2533,2722,3235,3253,3325,3352,3523,3532,5233,5323,5332,7222,22225,22252,22333,22522,23233,23323,23332,25222,32233,32323,32332,33223,33232,33322,52222,222223,222232,222322,223222,232222,322222]
</pre>
 
=={{header|Python}}==
With improvements to the ideas from the discussion page:
<syntaxhighlight lang="python">from collections import deque
 
def prime_digits_sum(r):
q = deque([(r, 0)])
while q:
r, n = q.popleft()
for d in 2, 3, 5, 7:
if d >= r:
if d == r: yield n + d
break
q.append((r - d, (n + d) * 10))
 
print(*prime_digits_sum(13))</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [] ' [ [ ] ]
[ behead
' [ 2 3 5 7 ] witheach
[ dip dup join
0 over witheach +
dup 13 = iff
[ drop nested
dip rot join
unrot conclude ]
done
11 > iff drop done
nested swap dip join ]
drop dup [] = until ]
swap witheach
[ 0 swap witheach
[ swap 10 * + ]
number$ nested join ]
60 wrap$</syntaxhighlight>
 
{{out}}
 
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235
3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252
22333 22522 23233 23323 23332 25222 32233 32323 32332 33223
33232 33322 52222 222223 222232 222322 223222 232222 322222
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>put join ', ', sort +*, unique flat
< 2 2 2 2 2 3 3 3 5 5 7 >.combinations
.grep( *.sum == 13 )
.map( { .join => $_ } )
.map: { .value.permutations».join }</langsyntaxhighlight>
{{Out}}
<pre>337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds and displays all decimal numbers whose digits are prime and sum to 13. */
parse arg LO= 337;HI COLS . HI= 322222; #= 0 /*define low&highobtain rangeoptional forarguments #s;from #the countCL*/
if LO=='' | LO=="," then LO= 337 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= 322222 /* " " " " " " */
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 10 /*width of a number in any column. */
title= ' decimal numbers found whose digits are prime and sum to 13 '
say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + cols*(w+1), '─')
w= 10 /*max width of a integer in any column.*/
found= 0; idx= 1 /*the number of numbers found (so far).*/
$= /*variable to hold the list of #s found*/
do j=LO for HI-LO+1 /*search for numbers in this range. */
Line 948 ⟶ 2,185:
sum= sum + substr(j, k, 1) /*sum some middle decimal digits of J.*/
end /*k*/
if sum\==13 then iterate then iterate /*Sum not equal to 13? Then skip this #*/
#found= #found + 1; $= $ j /*bump #the count;number appendof #numbers found. to the $ list.*/
c= commas(j) /*maybe add commas to the number. */
$= $ right( commas(j), w) /*add the found number ───► the $ list.*/
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
 
sayif strip($); \=='' then say center(idx, 7)"│" substr($, 2) /*possible display theresidual output list to the term. */
say '───────┴'center("" , 1 + cols*(w+1), '─')
say # ' decimal numbers found whose digits are prime and the decimal digits sum to 13'</lang>
say
say 'Found ' commas(found) title
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
index │ decimal numbers found whose digits are prime and sum to 13
337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ 337 355 373 535 553 733 2,227 2,272 2,335 2,353
11 │ 2,533 2,722 3,235 3,253 3,325 3,352 3,523 3,532 5,233 5,323
21 │ 5,332 7,222 22,225 22,252 22,333 22,522 23,233 23,323 23,332 25,222
31 │ 32,233 32,323 32,332 33,223 33,232 33,322 52,222 222,223 222,232 222,322
41 │ 223,222 232,222 322,222
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────
 
Found 43 decimal numbers found whose digits are prime and the decimal digits sum to 13
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 996 ⟶ 2,250:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,002 ⟶ 2,256:
[337,355,373,535,553,733,2227,2272,2335,2353,2533,2722,3235,3253,3325,3352,3523,3532,5233,5323,5332,7222,22225,22252,22333,22522,23233,23323,23332,25222,32233,32323,32332,33223,33232,33322,52222,222223,222232,222322,223222,232222,322222]
</pre>
 
=={{header|RPL}}==
Nice recursive solution from the discussion page:
{{works with|HP|49}}
« { } { "" }
'''DO''' { }
1 PICK3 SIZE '''FOR''' j
2 7 '''FOR''' d
OVER j GET d +
0
1 PICK3 SIZE '''FOR''' k
OVER k DUP SUB STR→ +
'''NEXT'''
'''CASE'''
DUP 13 == '''THEN''' DROP 4 ROLL SWAP + UNROT '''END'''
11 > '''THEN''' DROP '''END'''
+
'''END'''
d 2 ≠ 1 + '''STEP''' <span style="color:grey">@ generates 2 3 5 7 index sequence</span>
'''NEXT''' NIP
'''UNTIL''' DUP SIZE NOT '''END'''
DROP
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222 }
</pre>
 
=={{header|Ruby}}==
{{trans|C}}
<syntaxhighlight lang="ruby">def primeDigitsSum13(n)
sum = 0
while n > 0
r = n % 10
if r != 2 and r != 3 and r != 5 and r != 7 then
return false
end
n = (n / 10).floor
sum = sum + r
end
return sum == 13
end
 
c = 0
for i in 1 .. 1000000
if primeDigitsSum13(i) then
print "%6d " % [i]
if c == 10 then
c = 0
print "\n"
else
c = c + 1
end
end
end
print "\n"
</syntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222
22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332
33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func generate_from_prefix(sum, p, base, digits) {
 
var seq = [p]
 
for d in (digits) {
seq << __FUNC__(sum, [d, p...], base, digits)... if (p.sum+d <= sum)
}
 
return seq
}
 
func numbers_with_digitsum(sum, base = 10, digits = (base-1 -> primes)) {
 
digits.map {|p| generate_from_prefix(sum, [p], base, digits)... }\
.map {|t| digits2num(t, base) }\
.grep {|t| t.sumdigits(base) == sum }\
.sort
}
 
say numbers_with_digitsum(13)</syntaxhighlight>
{{out}}
<pre>
[337, 355, 373, 535, 553, 733, 2227, 2272, 2335, 2353, 2533, 2722, 3235, 3253, 3325, 3352, 3523, 3532, 5233, 5323, 5332, 7222, 22225, 22252, 22333, 22522, 23233, 23323, 23332, 25222, 32233, 32323, 32332, 33223, 33232, 33322, 52222, 222223, 222232, 222322, 223222, 232222, 322222]
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">set res {}
set src [list {} 13]
while {[llength $src]} {
set src [lassign $src n r]
foreach d {2 3 5 7} {
if {$d >= $r} {
if {$d == $r} {lappend res "$n$d"}
break
}
lappend src "$n$d" [expr {$r - $d}]
}
}
puts $res</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh">set -- '' 13
res=''
while [ $# -ne 0 ]
do
for d in 2 3 5 7
do
[ $d -ge $2 ] && {
[ $d -eq $2 ] && res=$res${res:+ }$1$d
break
}
set -- "$@" $1$d $(($2 - d))
done
shift 2
done
echo "$res"</syntaxhighlight>
{{out}}
<pre>337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222</pre>
 
=={{header|Visual Basic .NET}}==
{{Trans|Phix}}
Same recursive method.
<langsyntaxhighlight lang="vbnet">Imports System
Imports System.Console
Imports LI = System.Collections.Generic.SortedSet(Of Integer)
Line 1,026 ⟶ 2,404:
unl(new LI From {}, new LI From { 2, 3, 5, 7 }, 13)))
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre style="white-space:pre-wrap;">337 355 373 535 553 733 2227 2272 2335 2353 2533 2722 3235 3253 3325 3352 3523 3532 5233 5323 5332 7222 22225 22252 22333 22522 23233 23323 23332 25222 32233 32323 32332 33223 33232 33322 52222 222223 222232 222322 223222 232222 322222
Line 1,032 ⟶ 2,410:
=== Alternate ===
Thanks to '''Nigel Galloway's''' suggestion from the discussion page.
<langsyntaxhighlight lang="vbnet">Imports Tu = System.Tuple(Of Integer, Integer)
 
Module Module1
Line 1,047 ⟶ 2,425:
End Sub
 
End Module</langsyntaxhighlight>
Same output.
 
Line 1,055 ⟶ 2,433:
{{libheader|Wren-sort}}
As the only digits which are prime are [2, 3, 5, 7], it is clear that a number must have between 3 and 6 digits for them to sum to 13.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
import "./seq" for Lst
import "./sort" for Sort
 
var combrep // recursive
Line 1,101 ⟶ 2,479:
Sort.quick(res)
System.print("Those numbers whose digits are all prime and sum to 13 are:")
System.print(res)</langsyntaxhighlight>
 
{{out}}
Line 1,110 ⟶ 2,488:
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">
<lang XPL0>
int N, M, S, D;
[for N:= 2 to 322222 do
Line 1,125 ⟶ 2,503:
until M=0; \all digits in N tested or digit not prime
];
]</langsyntaxhighlight>
 
{{out}}
1,962

edits