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

m
→‎{{header|Phix}}: use pygments
(Created page with "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: <lang Phix>funct...")
 
m (→‎{{header|Phix}}: use pygments)
 
(2 intermediate revisions by the same user not shown)
Line 2:
 
I decided to keep the main entry simple, and archived this OTT version here:
<!--(phixonline)-->
<lang Phix>function unlucky(sequence set, integer needed, atom mult=1, v=0, sequence res={})
<syntaxhighlight lang="Phix">
with javascript_semantics
<lang Phix>function unlucky(sequence set, integer needed, atom mult=1, v=0, sequence res={})
if needed=0 then
res = append(res,v)
Line 12 ⟶ 15:
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?)
Line 23 ⟶ 26:
end for
printf(1,"Prime_digit-only numbers summing to %d: %s\n",{p,join(s)})
end for</lang>
</syntaxhighlight>
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.
{{out}}
Line 44 ⟶ 48:
</pre>
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:Numbers_with_prime_digits_whose_sum_is_13|Talk page]]<br>
I am tempted to replace my original, as this is a bit cleaner and does not require a sort, but it is longer...
<syntaxhighlight lang="Phix">
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"})
</syntaxhighlight>
<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>
7,813

edits