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

Added Easylang
(Added Easylang)
 
(19 intermediate revisions by 9 users not shown)
Line 5:
=={{header|11l}}==
{{trans|C}}
<syntaxhighlight lang="11l">F primeDigitsSum13(=n)
 
<lang 11l>F primeDigitsSum13(=n)
V sum = 0
L n > 0
Line 23 ⟶ 22:
c = 0
print()
print()</langsyntaxhighlight>
 
{{out}}
Line 34 ⟶ 33:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PRIMECOUNT="4"
BYTE ARRAY primedigits(PRIMECOUNT)=[2 3 5 7]
 
Line 84 ⟶ 83:
BYTE ARRAY digits(MAXDIG)
BYTE count,pos
 
count=1 pos=0
Init(count)
Line 103 ⟶ 102:
FI
OD
RETURN</langsyntaxhighlight>
{{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]
Line 114 ⟶ 113:
=={{header|ALGOL 68}}==
Based on the Algol W sample.
<langsyntaxhighlight 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 #
Line 156 ⟶ 155:
OD # d2 #
OD # d1 #
END</langsyntaxhighlight>
{{out}}
<pre>
Line 167 ⟶ 166:
=={{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 201 ⟶ 200:
end for_d2
end for_d1
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 211 ⟶ 210:
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pDigits: [2 3 5 7]
 
<lang rebol>pDigits: [2 3 5 7]
lst: map pDigits 'd -> @[d]
result: new []
Line 234 ⟶ 232:
 
loop split.every: 10 result 'a ->
print map a => [pad to :string & 6]</langsyntaxhighlight>
 
{{out}}
Line 245 ⟶ 243:
 
=={{header|AWK}}==
===Counting and testing===
<lang AWK>
<syntaxhighlight lang="awk">
# syntax: GAWK -f NUMBERS_WITH_PRIME_DIGITS_WHOSE_SUM_IS_13.AWK
BEGIN {
Line 276 ⟶ 275:
return(sum == 13)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 284 ⟶ 283:
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
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 327 ⟶ 408:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 337 ⟶ 418:
{{Trans|Phix}}
Same recursive method.
<langsyntaxhighlight lang="csharp">using System;
using static System.Console;
using LI = System.Collections.Generic.SortedSet<int>;
Line 351 ⟶ 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 357 ⟶ 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 367 ⟶ 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 385 ⟶ 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}}
<langsyntaxhighlight lang="d">import std.stdio;
 
bool primeDigitsSum13(int n) {
Line 421 ⟶ 502:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 427 ⟶ 508:
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>
Line 443 ⟶ 550:
===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 450 ⟶ 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 456 ⟶ 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>
Line 472 ⟶ 673:
Ho hum. Another prime digits task.
 
<langsyntaxhighlight lang="freebasic">
function digit_is_prime( n as integer ) as boolean
select case n
Line 502 ⟶ 703:
for i as uinteger = 1 to 322222
if all_digits_prime(i) andalso digit_sum_13(i) then print i,
next i</langsyntaxhighlight>
{{out}}
<pre>
Line 517 ⟶ 718:
=={{header|Go}}==
Reuses code from some other tasks.
<langsyntaxhighlight lang="go">package main
 
import (
Line 590 ⟶ 791:
fmt.Println("Those numbers whose digits are all prime and sum to 13 are:")
fmt.Println(res2)
}</langsyntaxhighlight>
 
{{out}}
Line 599 ⟶ 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 688 ⟶ 889:
fmt.Println("The count of numbers whose digits are all prime and sum to",mySum,"is",gblCount)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 725 ⟶ 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 774 ⟶ 975:
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0</langsyntaxhighlight>
{{Out}}
<pre>43 numbers with prime digits summing to 13:
Line 783 ⟶ 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}}
<langsyntaxhighlight lang="java">public class PrimeDigits {
private static boolean primeDigitsSum13(int n) {
int sum = 0;
Line 814 ⟶ 1,038:
System.out.println();
}
}</langsyntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 823 ⟶ 1,047:
=={{header|JavaScript}}==
As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,003 ⟶ 1,227:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>337,355,373,535,553,733
Line 1,018 ⟶ 1,242:
'''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.
Two solutions are presented. Both are based on the
 
observation that the only digits which are prime are [2, 3, 5, 7]
All three solutions are based on the observation that the only decimal digits which are prime are [2, 3, 5, 7].
so the numbers of interest cannot have more than 6 digits.
 
To save space, both only present the count of the number of solutions; this is done using the stream counter:
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'''====
<langsyntaxhighlight lang="jq"># Output: a stream
def simple:
range(2; 7) as $n
Line 1,034 ⟶ 1,259:
| join("") | tonumber;
 
count(simple)</langsyntaxhighlight>
{{out}}
''A Faster Solution'''
<pre>
<lang jq>def faster:
43
</pre>
====A Faster Solution====
<syntaxhighlight lang="jq">def faster:
def digits: [2, 3, 5, 7];
def wide($max):
Line 1,051 ⟶ 1,280:
 
count(faster)
</syntaxhighlight>
</lang>
{{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}}
In both cases:
<pre>
43
349321957098598244959032342621956
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics, Primes
 
function primedigitsums(targetsum)
Line 1,106 ⟶ 1,375:
 
foreach(countprimedigitsums, nextprimes(17, 40))
</langsyntaxhighlight>{{out}}
<pre>
There are 3 prime-digit-only numbers summing to 5 : [5, 32, 23]
Line 1,156 ⟶ 1,425:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">fun primeDigitsSum13(n: Int): Boolean {
var nn = n
var sum = 0
Line 1,183 ⟶ 1,452:
}
println()
}</langsyntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 1,192 ⟶ 1,461:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function prime_digits_sum_13(n)
local sum = 0
while n > 0 do
Line 1,217 ⟶ 1,486:
end
end
print()</langsyntaxhighlight>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 1,225 ⟶ 1,494:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils
 
type Digit = 0..9
Line 1,252 ⟶ 1,521:
for i, n in result:
stdout.write ($n).align(6), if (i + 1) mod 9 == 0: '\n' else: ' '
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,260 ⟶ 1,529:
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 1,406 ⟶ 1,692:
writeln(num:6,gblCount:25,' ');
until num > MAXNUM;
END.</langsyntaxhighlight>
{{Out}}
<pre> Sum Count of arrangements
Line 1,442 ⟶ 1,728:
</pre>
===using gmp===
<langsyntaxhighlight lang="pascal">program PrimSumUpTo13_GMP;
{$IFDEF FPC}
{$OPTIMIZATION ON,ALL}
Line 1,657 ⟶ 1,943:
z_clear(gblSum);
z_clear(gbldelta);
END.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,711 ⟶ 1,997:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,730 ⟶ 2,016:
}
}
print $numbers =~ s/.{1,80}\K /\n/gr;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,739 ⟶ 2,025:
 
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #008080;">function</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">={})</span>
function unlucky(sequence set, integer needed, string v="", sequence res={})
<span style="color: #008080;">if</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if needed=0 then
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%6s"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">))</span>
res = append(res,sprintf("%6s",v))
<span style="color: #008080;">elsif</span> <span style="color: #000000;">needed</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
elsif needed>0 then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
for i=length(set) to 1 by -1 do
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">needed</span><span style="color: #0000FF;">-</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
res = unlucky(set,needed-set[i],(set[i]+'0')&v,res)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
sequence r = sort(unlucky({2,3,5,7},13))
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unlucky</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span><span style="color: #000000;">13</span><span style="color: #0000FF;">))</span>
puts(1,join_by(r,1,11," "))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
</syntaxhighlight>
<!--</lang>-->
{{out}}
<pre>
Line 1,764 ⟶ 2,050:
===iterative===
Queue-based version of Nigel's recursive algorithm, same output.
<!--<lang Phix>(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
with javascript_semantics
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- uses latest apply() mods, rest is fine</span>
requires("0.8.2") -- uses latest apply() mods, rest is fine
<span style="color: #008080;">constant</span> <span style="color: #000000;">dgts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}</span>
constant dgts = {2,3,5,7}
<span style="color: #008080;">function</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">()</span>
function unlucky()
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}}</span>
sequence res = {}, q = {{0,0}}
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- partial digit sum, &lt;=11</span>
integer s, -- partial digit sum, <=11
<span style="color: #000000;">v</span> <span style="color: #000080;font-style:italic;">-- corresponding value</span>
v -- corresponding value
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">q</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
while length(q) do
<span style="color: #0000FF;">{{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">q</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">q</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]}</span>
{{s,v}, q} = {q[1], q[2..$]}
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dgts</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i=1 to length(dgts) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dgts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ns</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nv</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">*</span><span style="color: #000000;">10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span>
integer d = dgts[i], {ns,nv} = {s+d,v*10+d}
<span style="color: #008080;">if</span> <span style="color: #000000;">ns</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">11</span> <span style="color: #008080;">then</span> <span style="color: #000000;">q</span> <span style="color: #0000FF;">&=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">ns</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nv</span><span style="color: #0000FF;">}}</span>
if ns<=11 then q &= {{ns,nv}}
<span style="color: #008080;">elsif</span> <span style="color: #000000;">ns</span><span style="color: #0000FF;">=</span><span style="color: #000000;">13</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">nv</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
elsif ns=13 then res &= nv end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end while
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
end function
sequence r = unlucky()
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unlucky</span><span style="color: #0000FF;">()</span>
r = apply(true,sprintf,{{"%6d"},r})
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%6d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
puts(1,join_by(r,1,11," "))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
</syntaxhighlight>
<!--</lang>-->
 
I've archived a slightly more OTT version: [[Numbers_with_prime_digits_whose_sum_is_13/Phix]].
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
digit_sum(N, M) :- digit_sum(N, 0, M).
digit_sum(0, A, B) :- !, A = B.
Line 1,816 ⟶ 2,102:
 
?- main.
</syntaxhighlight>
</lang>
{{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 HI COLS . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 337 /*Not specified? Then use the default.*/
Line 1,867 ⟶ 2,200:
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 _</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
Line 1,883 ⟶ 2,216:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,917 ⟶ 2,250:
next
? "[" + left(svect, len(svect) - 1) + "]"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Unlucky numbers 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|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}}
<langsyntaxhighlight lang="ruby">def primeDigitsSum13(n)
sum = 0
while n > 0
Line 1,952 ⟶ 2,312:
end
print "\n"
</syntaxhighlight>
</lang>
{{out}}
<pre> 337 355 373 535 553 733 2227 2272 2335 2353 2533
Line 1,960 ⟶ 2,320:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func generate_from_prefix(sum, p, base, digits) {
 
var seq = [p]
Line 1,979 ⟶ 2,339:
}
 
say numbers_with_digitsum(13)</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|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 2,008 ⟶ 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 2,014 ⟶ 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 2,029 ⟶ 2,425:
End Sub
 
End Module</langsyntaxhighlight>
Same output.
 
Line 2,037 ⟶ 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 2,083 ⟶ 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 2,092 ⟶ 2,488:
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">
<lang XPL0>
int N, M, S, D;
[for N:= 2 to 322222 do
Line 2,107 ⟶ 2,503:
until M=0; \all digits in N tested or digit not prime
];
]</langsyntaxhighlight>
 
{{out}}
1,969

edits