Zumkeller numbers: Difference between revisions
m
→{{header|Phix}}: use pygments (and fix the misplaced dblquote which made it look empty), use odd(), even(), and %n
m (→{{header|Wren}}: Minor tidy) |
m (→{{header|Phix}}: use pygments (and fix the misplaced dblquote which made it look empty), use odd(), even(), and %n) |
||
Line 4,012:
=={{header|Phix}}==
{{trans|Go}}
<!--
with javascript_semantics
function isPartSum(sequence f, integer l, t)
if t=0 then return true end if
if l=0 then return false end if
integer last = f[l]
return (t>=last and isPartSum(f, l-1, t-last))
or isPartSum(f, l-1, t)
end function
function isZumkeller(integer n)
sequence f = factors(n,1)
<span style="color: #000080;font-style:italic;">-- an odd sum cannot be split into two equal sums</span>▼
integer t = sum(f)
▲
<span style="color: #000080;font-style:italic;">-- if n is odd use 'abundant odd number' optimization</span>▼
if odd(t) then return false end if
▲
if odd(n) then
integer abundance := t - 2*n
return abundance>0 and even(abundance)
<span style="color: #000080;font-style:italic;">-- if n and t both even check for any partition of t/2</span>▼
end if
▲
return isPartSum(f, length(f), t/2)
end function
sequence tests = {{220,1,0,20,"%3d %n"},
{40,2,0,10,"%5d %n"},
{40,2,5,8,"%7d %n"}}
integer lim, step, rem, cr; string fmt
for t=1 to length(tests) do
{lim, step, rem, cr, fmt} = tests[t]
string o = iff(step=1?"":"odd "),
w = iff(rem=0?"":"which don't end in 5 ")
printf(1,"The first %d %sZumkeller numbers %sare:\n",{lim,o,w})
integer i = step+1, count = 0
while count<lim do
if (rem=0 or remainder(i,10)!=rem)
and isZumkeller(i) then
count += 1
printf(1,fmt,{i,remainder(count,cr)=0})
end if
i += step
end while
printf(1,"\n")
end for
▲<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
|