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

m
→‎{{header|Phix}}: use pygments
m (added an alternative)
m (→‎{{header|Phix}}: use pygments)
 
(One intermediate revision 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 48 ⟶ 52:
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">
<lang Phix>constant digits = {2,3,5,7}
with javascript_semantics
<lang Phix>constant digits = {2,3,5,7}
function unlucky(sequence part)
sequence res={}, next={}
Line 68 ⟶ 74:
end function
pp(unlucky({{0,0}}),{pp_IntFmt,"%7d"})</lang>
</syntaxhighlight>
<pre>
{ 337, 355, 373, 535, 553, 733, 2227, 2272, 2335,
7,818

edits