Numbers with prime digits whose sum is 13/Phix

From Rosetta Code

Extended Phix version of Numbers_with_prime_digits_whose_sum_is_13#Phix.

I decided to keep the main entry simple, and archived this OTT version here:

with javascript_semantics
function unlucky(sequence set, integer needed, atom mult=1, v=0, sequence res={})
    if needed=0 then
        res = append(res,v)
    elsif needed>0 then
        for i=length(set) to 1 by -1 do
            res = unlucky(set,needed-set[i],mult*10,v+set[i]*mult,res)
        end for
    end if
    return res
end function            
 
for i=6 to 6 do -- (see below)
    integer p = get_prime(i)
    sequence r = sort(unlucky({2,3,5,7},p)),
             s = deep_copy(shorten(r,"numbers",3))
    integer l = length(s),
            m = l<length(r) -- (ie shortened?)
    for j=1 to l-m do
        if s[j]!="..." then s[j] = sprintf("%d",s[j]) end if
    end for
    printf(1,"Prime_digit-only numbers summing to %d: %s\n",{p,join(s)})
end for

Originally I thought I wouldn't need to sort the output of unlucky(), but it generates all numbers ending in 7 first, and alas (eg) 355 < 2227, not that it hurts any.

Output:
Prime_digit-only numbers summing to 13: 337 355 373 ... 223222 232222 322222  (43 numbers)

With "for i=1 to 11" you get:

Prime_digit-only numbers summing to 2: 2
Prime_digit-only numbers summing to 3: 3
Prime_digit-only numbers summing to 5: 5 23 32
Prime_digit-only numbers summing to 7: 7 25 52 223 232 322
Prime_digit-only numbers summing to 11: 227 272 335 ... 22322 23222 32222  (19 numbers)
Prime_digit-only numbers summing to 13: 337 355 373 ... 223222 232222 322222  (43 numbers)
Prime_digit-only numbers summing to 17: 377 557 575 ... 22322222 23222222 32222222  (221 numbers)
Prime_digit-only numbers summing to 19: 577 757 775 ... 223222222 232222222 322222222  (468 numbers)
Prime_digit-only numbers summing to 23: 2777 7277 7727 ... 22322222222 23222222222 32222222222  (2,098 numbers)
Prime_digit-only numbers summing to 29: 35777 37577 37757 ... 22322222222222 23222222222222 32222222222222  (21,049 numbers)
Prime_digit-only numbers summing to 31: 37777 55777 57577 ... 223222222222222 232222222222222 322222222222222  (45,148 numbers)

Note that the largest sum-to-37, 322222222222222222, being as it is 18 digits long, exceeds the capacity of a 64-bit float.

Alternative

Based on the algorthim suggested by Nigel Galloway on the Talk page
I am tempted to replace my original, as this is a bit cleaner and does not require a sort, but it is longer...

with javascript_semantics
constant digits = {2,3,5,7}
function unlucky(sequence part)
    sequence res={}, next={}
    for p=1 to length(part) do
        integer {v,s} = part[p]
        for i=1 to length(digits) do
            integer d = digits[i],
                    sn = d+s,
                    nv = v*10+d
            if sn=13 then
                res &= nv
            elsif sn<=11 then
                next &= {{nv,sn}}
            end if
        end for
    end for
    if length(next) then res &= unlucky(next) end if
    return res
end function
 
pp(unlucky({{0,0}}),{pp_IntFmt,"%7d"})
{    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}