I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Numbers with prime digits whose sum is 13

From Rosetta Code
Numbers with prime digits whose sum is 13 is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Find all the decimal numbers whose digits are all primes and sum to 13.


ALGOL W[edit]

Uses the observations about the digits and numbers in the Wren solution to generate the sequence.

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  %
integer numberCount;
numberCount := 0;
write();
for d1 := 0, 2, 3, 5, 7 do begin
for d2 := 0, 2, 3, 5, 7 do begin
if d2 not = 0 or d1 = 0 then begin
for d3 := 0, 2, 3, 5, 7 do begin
if d3 not = 0 or ( d1 = 0 and d2 = 0 ) then begin
for d4 := 2, 3, 5, 7 do begin
for d5 := 2, 3, 5, 7 do begin
for d6 := 2, 3, 5, 7 do begin
integer sum;
sum := d1 + d2 + d3 + d4 + d5 + d6;
if sum = 13 then begin
 % found a number whose prime digits sum to 13 %
integer n;
n := 0;
for d := d1, d2, d3, d4, d5, d6 do n := ( n * 10 ) + d;
writeon( i_w := 6, s_w := 1, n );
numberCount := numberCount + 1;
if numberCount rem 12 = 0 then write()
end if_sum_eq_13
end for_d6
end for_d5
end for_d4
end if_d3_ne_0_or_d1_eq_0_and_d2_e_0
end for_d3
end if_d2_ne_0_or_d1_eq_0
end for_d2
end for_d1
end.
Output:
   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

C#[edit]

Translation of: Phix

Same recursive method.

using System;
using static System.Console;
using LI = System.Collections.Generic.SortedSet<int>;
 
class Program {
 
static LI unl(LI res, LI set, int lft, int mul = 1, int vlu = 0) {
if (lft == 0) res.Add(vlu);
else if (lft > 0) foreach (int itm in set)
res = unl(res, set, lft - itm, mul * 10, vlu + itm * mul);
return res; }
 
static void Main(string[] args) { WriteLine(string.Join(" ",
unl(new LI {}, new LI { 2, 3, 5, 7 }, 13))); }
}
Output:
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

Alternate[edit]

Based in Nigel Galloway's suggestion from the discussion page.

class Program {
 
static void Main(string[] args) { int[] lst; int sum;
var w = new System.Collections.Generic.List<(int digs, int sum)> {};
foreach (int x in lst = new int[] { 2, 3, 5, 7 } ) w.Add((x, x));
while (w.Count > 0) { var i = w[0]; w.RemoveAt(0);
foreach (var j in lst) if ((sum = i.sum + j) == 13)
System.Console.Write ("{0}{1} ", i.digs, j);
else if (sum < 12)
w.Add((i.digs * 10 + j, sum)); } }
}

Same output.

C++[edit]

Translation of: C#
(the alternate version)
#include <cstdio>
#include <vector>
#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
vector<tuple<int, int>> w; int lst[4] = { 2, 3, 5, 7 }, sum;
for (int x : lst) w.push_back({x, x});
while (w.size() > 0) { auto i = w[0]; w.erase(w.begin());
for (int x : lst) if ((sum = get<1>(i) + x) == 13)
printf("%d%d ", get<0>(i), x);
else if (sum < 12) w.push_back({get<0>(i) * 10 + x, sum}); }
return 0; }

Same output as C#.

F#[edit]

 
// 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 ""
 
Output:
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

Factor[edit]

Filtering selections[edit]

Generate all selections of the prime digits in the only possible lengths whose sum can be 13, then filter for sums that equal 13.

USING: formatting io kernel math math.combinatorics
math.functions math.ranges sequences sequences.extras ;
 
: digits>number ( seq -- n ) reverse 0 [ 10^ * + ] reduce-index ;
 
"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
Output:
Numbers 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 }

F# translation[edit]

The following is based on Nigel Galloway's algorithm as described here on the talk page. It's about 10x faster than the previous method.

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
Output:
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

Go[edit]

Reuses code from some other tasks.

package main
 
import (
"fmt"
"sort"
"strconv"
)
 
func combrep(n int, lst []byte) [][]byte {
if n == 0 {
return [][]byte{nil}
}
if len(lst) == 0 {
return nil
}
r := combrep(n, lst[1:])
for _, x := range combrep(n-1, lst) {
r = append(r, append(x, lst[0]))
}
return r
}
 
func shouldSwap(s []byte, start, curr int) bool {
for i := start; i < curr; i++ {
if s[i] == s[curr] {
return false
}
}
return true
}
 
func findPerms(s []byte, index, n int, res *[]string) {
if index >= n {
*res = append(*res, string(s))
return
}
for i := index; i < n; i++ {
check := shouldSwap(s, index, i)
if check {
s[index], s[i] = s[i], s[index]
findPerms(s, index+1, n, res)
s[index], s[i] = s[i], s[index]
}
}
}
 
func main() {
primes := []byte{2, 3, 5, 7}
var res []string
for n := 3; n <= 6; n++ {
reps := combrep(n, primes)
for _, rep := range reps {
sum := byte(0)
for _, r := range rep {
sum += r
}
if sum == 13 {
var perms []string
for i := 0; i < len(rep); i++ {
rep[i] += 48
}
findPerms(rep, 0, len(rep), &perms)
res = append(res, perms...)
}
}
}
res2 := make([]int, len(res))
for i, r := range res {
res2[i], _ = strconv.Atoi(r)
}
sort.Ints(res2)
fmt.Println("Those numbers whose digits are all prime and sum to 13 are:")
fmt.Println(res2)
}
Output:
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]

only counting[edit]

See Julia [1]

 
package main
 
import (
"fmt"
)
var
Primes = []byte{2, 3, 5, 7};
var
gblCount = 0;
var
PrimesIdx = []byte{0, 1, 2, 3};
 
func combrep(n int, lst []byte) [][]byte {
if n == 0 {
return [][]byte{nil}
}
if len(lst) == 0 {
return nil
}
r := combrep(n, lst[1:])
for _, x := range combrep(n-1, lst) {
r = append(r, append(x, lst[0]))
}
return r
}
 
func Count(rep []byte)int {
var PrimCount [4]int
for i := 0; i < len(PrimCount); i++ {
PrimCount[i] = 0;
}
//get the count of every item
for i := 0; i < len(rep); i++ {
PrimCount[rep[i]]++
}
var numfac int = len(rep)
 
var numerator,denominator[]int
 
for i := 1; i <= len(rep); i++ {
numerator = append(numerator,i) // factors 1,2,3,4. n
denominator = append(denominator,1)
}
numfac = 0; //idx in denominator
for i := 0; i < len(PrimCount); i++ {
denfac := 1;
for j := 0; j < PrimCount[i]; j++ {
denominator[numfac] = denfac
denfac++
numfac++
}
}
//calculate permutations with identical items
numfac = 1;
for i := 0; i < len(numerator); i++ {
numfac = (numfac * numerator[i])/denominator[i]
}
return numfac
}
 
func main() {
for mySum := 2; mySum <= 103;mySum++ {
gblCount = 0;
//check for prime
for i := 2; i*i <= mySum;i++{
if mySum%i == 0 {
gblCount=1;
break
}
}
if gblCount != 0 {
continue
}
 
for n := 1; n <= mySum / 2 ; n++ {
reps := combrep(n, PrimesIdx)
for _, rep := range reps {
sum := byte(0)
for _, r := range rep {
sum += Primes[r]
}
if sum == byte(mySum) {
gblCount+=Count(rep);
}
}
}
fmt.Println("The count of numbers whose digits are all prime and sum to",mySum,"is",gblCount)
}
}
Output:
The count of numbers whose digits are all prime and sum to 2 is 1
The count of numbers whose digits are all prime and sum to 3 is 1
The count of numbers whose digits are all prime and sum to 5 is 3
The count of numbers whose digits are all prime and sum to 7 is 6
The count of numbers whose digits are all prime and sum to 11 is 19
The count of numbers whose digits are all prime and sum to 13 is 43
The count of numbers whose digits are all prime and sum to 17 is 221
The count of numbers whose digits are all prime and sum to 19 is 468
The count of numbers whose digits are all prime and sum to 23 is 2098
The count of numbers whose digits are all prime and sum to 29 is 21049
The count of numbers whose digits are all prime and sum to 31 is 45148
The count of numbers whose digits are all prime and sum to 37 is 446635
The count of numbers whose digits are all prime and sum to 41 is 2061697
The count of numbers whose digits are all prime and sum to 43 is 4427752
The count of numbers whose digits are all prime and sum to 47 is 20424241
The count of numbers whose digits are all prime and sum to 53 is 202405001
The count of numbers whose digits are all prime and sum to 59 is 2005642061
The count of numbers whose digits are all prime and sum to 61 is 4307930784
The count of numbers whose digits are all prime and sum to 67 is 42688517778
The count of numbers whose digits are all prime and sum to 71 is 196942068394
The count of numbers whose digits are all prime and sum to 73 is 423011795680
The count of numbers whose digits are all prime and sum to 79 is 4191737820642
The count of numbers whose digits are all prime and sum to 83 is 19338456915087
The count of numbers whose digits are all prime and sum to 89 is 191629965405641
The count of numbers whose digits are all prime and sum to 97 is 4078672831913824
The count of numbers whose digits are all prime and sum to 101 is 18816835854129198
The count of numbers whose digits are all prime and sum to 103 is 40416663565084464

real  0m4,489s
user  0m5,584s
sys 0m0,188s

Haskell[edit]

As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.

import Data.List.Split (chunksOf)
import Data.List (intercalate, transpose, unfoldr)
import Text.Printf
 
primeDigitsNumsSummingToN :: Int -> [Int]
primeDigitsNumsSummingToN n = concat $ unfoldr go (return <$> primeDigits)
where
primeDigits = [2, 3, 5, 7]
 
go :: [[Int]] -> Maybe ([Int], [[Int]])
go xs
| null xs = Nothing
| otherwise = Just (nextLength xs)
 
nextLength :: [[Int]] -> ([Int], [[Int]])
nextLength xs =
let harvest nv =
[ unDigits $ fst nv
| n == snd nv ]
prune nv =
[ fst nv
| pred n > snd nv ]
in ((,) . concatMap harvest <*> concatMap prune)
(((,) <*> sum) <$> ((<$> xs) . (<>) . return =<< primeDigits))
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
let n = 13
xs = primeDigitsNumsSummingToN n
mapM_
putStrLn
[ concat
[ (show . length) xs
, " numbers with prime digits summing to "
, show n
, ":\n"
]
, table " " $ chunksOf 10 (show <$> xs)
]
 
table :: String -> [[String]] -> String
table gap rows =
let ic = intercalate
ws = maximum . fmap length <$> transpose rows
pw = printf . flip ic ["%", "s"] . show
in unlines $ ic gap . zipWith pw ws <$> rows
 
unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0
Output:
43 numbers with prime digits summing 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

JavaScript[edit]

As an unfold, in the recursive pattern described by Nigel Galloway on the Talk page.

(() => {
'use strict';
 
// ---- NUMBERS WITH PRIME DIGITS WHOSE SUM IS 13 ----
 
// primeDigitsSummingToN :: Int -> [Int]
const primeDigitsSummingToN = n => {
const primeDigits = [2, 3, 5, 7];
const go = xs =>
fanArrow(
concatMap( // Harvested,
nv => n === nv[1] ? (
[unDigits(nv[0])]
) : []
)
)(
concatMap( // Pruned.
nv => pred(n) > nv[1] ? (
[nv[0]]
) : []
)
)(
// Existing numbers with prime digits appended,
// tupled with the resulting digit sums.
xs.flatMap(
ds => primeDigits.flatMap(d => [
fanArrow(identity)(sum)(
ds.concat(d)
)
])
)
);
return concat(
unfoldr(
xs => 0 < xs.length ? (
Just(go(xs))
) : Nothing()
)(
primeDigits.map(pureList)
)
);
}
 
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
chunksOf(6)(
primeDigitsSummingToN(13)
).forEach(
x => console.log(x)
)
 
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// Just :: a -> Maybe a
const Just = x => ({
type: 'Maybe',
Nothing: false,
Just: x
});
 
 
// Nothing :: Maybe a
const Nothing = () => ({
type: 'Maybe',
Nothing: true,
});
 
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n =>
xs => enumFromThenTo(0)(n)(
xs.length - 1
).reduce(
(a, i) => a.concat([xs.slice(i, (n + i))]),
[]
);
 
 
// concat :: [[a]] -> [a]
// concat :: [String] -> String
const concat = xs => (
ys => 0 < ys.length ? (
ys.every(Array.isArray) ? (
[]
) : ''
).concat(...ys) : ys
)(list(xs));
 
 
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f =>
xs => xs.flatMap(f)
 
 
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = x1 =>
x2 => y => {
const d = x2 - x1;
return Array.from({
length: Math.floor(y - x2) / d + 2
}, (_, i) => x1 + (d * i));
};
 
 
// fanArrow (&&&) :: (a -> b) -> (a -> c) -> (a -> (b, c))
const fanArrow = f =>
// A function from x to a tuple of (f(x), g(x))
// ((,) . f <*> g)
g => x => Tuple(f(x))(
g(x)
);
 
 
// identity :: a -> a
const identity = x =>
// The identity function.
x;
 
 
// list :: StringOrArrayLike b => b -> [a]
const list = xs =>
// xs itself, if it is an Array,
// or an Array derived from xs.
Array.isArray(xs) ? (
xs
) : Array.from(xs || []);
 
 
// pred :: Enum a => a -> a
const pred = x =>
x - 1;
 
 
// pureList :: a -> [a]
const pureList = x => [x];
 
// sum :: [Num] -> Num
const sum = xs =>
// The numeric sum of all values in xs.
xs.reduce((a, x) => a + x, 0);
 
 
// unDigits :: [Int] -> Int
const unDigits = ds =>
// The integer with the given digits.
ds.reduce((a, x) => 10 * a + x, 0);
 
 
// The 'unfoldr' function is a *dual* to 'foldr':
// while 'foldr' reduces a list to a summary value,
// 'unfoldr' builds a list from a seed value.
 
// unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
const unfoldr = f =>
v => {
const xs = [];
let xr = [v, v];
while (true) {
const mb = f(xr[1]);
if (mb.Nothing) {
return xs;
} else {
xr = mb.Just;
xs.push(xr[0]);
}
}
};
 
return main();
})();
Output:
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

Julia[edit]

using Combinatorics, Primes
 
function primedigitsums(targetsum)
 
possibles = mapreduce(x -> fill(x, div(targetsum, x)), vcat, [2, 3, 5, 7])
 
a = map(x -> evalpoly(BigInt(10), x),
mapreduce(x -> unique(collect(permutations(x))), vcat,
unique(filter(x -> sum(x) == targetsum, collect(combinations(possibles))))))
 
println("There are $(length(a)) prime-digit-only numbers summing to $targetsum : $a")
 
end
 
foreach(primedigitsums, [5, 7, 11, 13])
 
function primedigitcombos(targetsum)
possibles = [2, 3, 5, 7]
found = Vector{Vector{Int}}()
combos = [Int[]]
tempcombos = Vector{Vector{Int}}()
newcombos = Vector{Vector{Int}}()
while !isempty(combos)
for combo in combos, j in possibles
csum = sum(combo) + j
if csum <= targetsum
newcombo = sort!([combo; j])
csum < targetsum && !(newcombo in newcombos) && push!(newcombos, newcombo)
csum == targetsum && !(newcombo in found) && push!(found, newcombo)
end
end
empty!(combos)
tempcombos = combos
combos = newcombos
newcombos = tempcombos
end
return found
end
 
function countprimedigitsums(targetsum)
found = primedigitcombos(targetsum)
total = sum(arr -> factorial(BigInt(length(arr))) ÷
prod(x -> factorial(BigInt(count(y -> y == x, arr))), unique(arr)), found)
println("There are $total prime-digit-only numbers summing to $targetsum.")
end
 
foreach(countprimedigitsums, nextprimes(17, 40))
 
Output:
There are 3 prime-digit-only numbers summing to 5 : [5, 32, 23]
There are 6 prime-digit-only numbers summing to 7 : [7, 52, 25, 322, 232, 223]
There are 19 prime-digit-only numbers summing to 11 : [722, 272, 227, 533, 353, 335, 5222, 2522, 2252, 2225, 3332, 3323, 3233, 2333, 32222, 23222, 22322, 22232, 22223]
There are 43 prime-digit-only numbers summing to 13 : [733, 373, 337, 553, 535, 355, 7222, 2722, 2272, 2227, 5332, 3532, 3352, 5323, 3523, 5233, 2533, 3253, 2353, 3325, 3235, 2335, 52222, 25222, 22522, 22252, 22225, 33322, 33232, 32332, 23332, 33223, 32323, 23323, 32233, 23233, 22333, 322222, 232222, 223222, 222322, 222232, 222223]
There are 221 prime-digit-only numbers summing to 17.
There are 468 prime-digit-only numbers summing to 19.
There are 2098 prime-digit-only numbers summing to 23.
There are 21049 prime-digit-only numbers summing to 29.
There are 45148 prime-digit-only numbers summing to 31.
There are 446635 prime-digit-only numbers summing to 37.
There are 2061697 prime-digit-only numbers summing to 41.
There are 4427752 prime-digit-only numbers summing to 43.
There are 20424241 prime-digit-only numbers summing to 47.
There are 202405001 prime-digit-only numbers summing to 53.
There are 2005642061 prime-digit-only numbers summing to 59.
There are 4307930784 prime-digit-only numbers summing to 61.
There are 42688517778 prime-digit-only numbers summing to 67.
There are 196942068394 prime-digit-only numbers summing to 71.
There are 423011795680 prime-digit-only numbers summing to 73.
There are 4191737820642 prime-digit-only numbers summing to 79.
There are 19338456915087 prime-digit-only numbers summing to 83.
There are 191629965405641 prime-digit-only numbers summing to 89.
There are 4078672831913824 prime-digit-only numbers summing to 97.
There are 18816835854129198 prime-digit-only numbers summing to 101.
There are 40416663565084464 prime-digit-only numbers summing to 103.
There are 186461075642340151 prime-digit-only numbers summing to 107.
There are 400499564627237889 prime-digit-only numbers summing to 109.
There are 1847692833654336940 prime-digit-only numbers summing to 113.
There are 389696778451488128521 prime-digit-only numbers summing to 127.
There are 1797854500757846669066 prime-digit-only numbers summing to 131.
There are 17815422682488317051838 prime-digit-only numbers summing to 137.
There are 38265729200380568226735 prime-digit-only numbers summing to 139.
There are 1749360471151229472803187 prime-digit-only numbers summing to 149.
There are 3757449669085729778349997 prime-digit-only numbers summing to 151.
There are 37233577041224219717325533 prime-digit-only numbers summing to 157.
There are 368957506121989278337474430 prime-digit-only numbers summing to 163.
There are 1702174484494837917764813972 prime-digit-only numbers summing to 167.
There are 16867303726643249517987636148 prime-digit-only numbers summing to 173.
There are 167142638782573042636172836062 prime-digit-only numbers summing to 179.
There are 359005512666242240589945886415 prime-digit-only numbers summing to 181.
There are 16412337250779890525195727788488 prime-digit-only numbers summing to 191.
There are 35252043354611887665339338710961 prime-digit-only numbers summing to 193.
There are 162634253887997896351270835136345 prime-digit-only numbers summing to 197.
There are 349321957098598244959032342621956 prime-digit-only numbers summing to 199.

Phix[edit]

function unlucky(sequence set, integer needed, string v="", sequence res={})
if needed=0 then
res = append(res,sprintf("%6s",v))
elsif needed>0 then
for i=length(set) to 1 by -1 do
res = unlucky(set,needed-set[i],(set[i]+'0')&v,res)
end for
end if
return res
end function
 
sequence r = sort(unlucky({2,3,5,7},13))
puts(1,join_by(r,1,11," "))
Output:
   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

iterative[edit]

Queue-based version of Nigel's recursive algorithm, same output.

requires("0.8.2") -- uses latest apply() mods, rest is fine
constant dgts = {2,3,5,7}
function unlucky()
sequence res = {}, q = {{0,0}}
integer s, -- partial digit sum, <=11
v -- corresponding value
while length(q) do
{{s,v}, q} = {q[1], q[2..$]}
for i=1 to length(dgts) do
integer d = dgts[i], {ns,nv} = {s+d,v*10+d}
if ns<=11 then q &= {{ns,nv}}
elsif ns=13 then res &= nv end if
end for
end while
return res
end function
 
sequence r = unlucky()
r = apply(true,sprintf,{{"%6d"},r})
puts(1,join_by(r,1,11," "))

I've archived a slightly more OTT version: Numbers_with_prime_digits_whose_sum_is_13/Phix.

Raku[edit]

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 }
Output:
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

REXX[edit]

/*REXX pgm finds and displays all decimal numbers whose digits are prime and sum to 13. */
LO= 337; HI= 322222; #= 0 /*define low&high range for #s; # count*/
$= /*variable to hold the list of #s found*/
do j=LO for HI-LO+1 /*search for numbers in this range. */
if verify(j, 2357) \== 0 then iterate /*J must be comprised of prime digits.*/
parse var j a 2 b 3 '' -1 z /*parse: 1st, 2nd, & last decimal digs.*/
sum= a + b + z /*sum: " " " " " " */
do k=3 for length(j)-3 /*only need to sum #s with #digits ≥ 4 */
sum= sum + substr(j, k, 1) /*sum some middle decimal digits of J.*/
end /*k*/
if sum\==13 then iterate /*Sum not equal to 13? Then skip this #*/
#= # + 1; $= $ j /*bump # count; append # to the $ list.*/
end /*j*/
 
say strip($); say /*display the output list to the term. */
say # ' decimal numbers found whose digits are prime and the decimal digits sum to 13'
output   when using the internal default inputs:
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

43  decimal numbers found whose digits are prime and the decimal digits sum to  13

Ring[edit]

 
load "stdlib.ring"
 
sum = 0
limit = 1000000
aPrimes = []
 
for n = 1 to limit
sum = 0
st = string(n)
for m = 1 to len(st)
num = number(st[m])
if isprime(num)
sum = sum + num
flag = 1
else
flag = 0
exit
ok
next
if flag = 1 and sum = 13
add(aPrimes,n)
ok
next
 
see "Unlucky numbers are:" + nl
see showArray(aPrimes)
 
func showarray vect
svect = ""
for n in vect
svect += "" + n + ","
next
 ? "[" + left(svect, len(svect) - 1) + "]"
 
Output:
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]

Visual Basic .NET[edit]

Translation of: Phix

Same recursive method.

Imports System
Imports System.Console
Imports LI = System.Collections.Generic.SortedSet(Of Integer)
 
Module Module1
Function unl(ByVal res As LI, ByVal lst As LI, ByVal lft As Integer, ByVal Optional mul As Integer = 1, ByVal Optional vlu As Integer = 0) As LI
If lft = 0 Then
res.Add(vlu)
ElseIf lft > 0 Then
For Each itm As Integer In lst
res = unl(res, lst, lft - itm, mul * 10, vlu + itm * mul)
Next
End If
Return res
End Function
 
Sub Main(ByVal args As String())
WriteLine(string.Join(" ",
unl(new LI From {}, new LI From { 2, 3, 5, 7 }, 13)))
End Sub
End Module
Output:
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

Alternate[edit]

Thanks to Nigel Galloway's suggestion from the discussion page.

Imports Tu = System.Tuple(Of Integer, Integer) 
 
Module Module1
 
Sub Main()
Dim w As New List(Of Tu), sum, x As Integer,
lst() As Integer = { 2, 3, 5, 7 }
For Each x In lst : w.Add(New Tu(x, x)) : Next
While w.Count > 0 : With w(0) : For Each j As Integer In lst
sum = .Item2 + j
If sum = 13 Then Console.Write("{0}{1} ", .Item1, j)
If sum < 12 Then w.Add(New Tu(.Item1 * 10 + j, sum))
Next : End With : w.RemoveAt(0) : End While
End Sub
 
End Module

Same output.

Wren[edit]

Library: Wren-math
Library: Wren-seq
Library: 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.

import "/math" for Nums
import "/seq" for Lst
import "/sort" for Sort
 
var combrep // recursive
combrep = Fn.new { |n, lst|
if (n == 0 ) return [[]]
if (lst.count == 0) return []
var r = combrep.call(n, lst[1..-1])
for (x in combrep.call(n-1, lst)) {
var y = x.toList
y.add(lst[0])
r.add(y)
}
return r
}
 
var permute // recursive
permute = Fn.new { |input|
if (input.count == 1) return [input]
var perms = []
var toInsert = input[0]
for (perm in permute.call(input[1..-1])) {
for (i in 0..perm.count) {
var newPerm = perm.toList
newPerm.insert(i, toInsert)
perms.add(newPerm)
}
}
return perms
}
 
var primes = [2, 3, 5, 7]
var res = []
for (n in 3..6) {
var reps = combrep.call(n, primes)
for (rep in reps) {
if (Nums.sum(rep) == 13) {
var perms = permute.call(rep)
for (i in 0...perms.count) perms[i] = Num.fromString(perms[i].join())
res.addAll(Lst.distinct(perms))
}
}
}
Sort.quick(res)
System.print("Those numbers whose digits are all prime and sum to 13 are:")
System.print(res)
Output:
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]

XPL0[edit]

 
int N, M, S, D;
[for N:= 2 to 322222 do
[M:= N; S:= 0;
repeat M:= M/10; \get digit D
D:= remainder(0);
case D of
2, 3, 5, 7:
[S:= S+D;
if S=13 and M=0 \all digits included\ then
[IntOut(0, N); ChOut(0, ^ )];
]
other M:= 0; \digit not prime so exit repeat loop
until M=0; \all digits in N tested or digit not prime
];
]
Output:
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