Zumkeller numbers: Difference between revisions
m
→{{header|EasyLang}}
(2 intermediate revisions by 2 users not shown) | |||
Line 2,010:
return 0
.
last = divs[
len divs[] -1
if last > sum
Line 2,034:
return ispartsum divs[] (sum / 2)
.
#
print "The first 220 Zumkeller numbers are:"
i = 2
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>
Line 5,713 ⟶ 5,714:
{{libheader|Wren-fmt}}
I've reversed the order of the recursive calls in the last line of the ''isPartSum'' function which, as noted in the Phix entry, seems to make little difference to Go but (as one might have expected) speeds up this Wren script enormously. The first part is now near instant but was taking several minutes previously. Overall it's now only about 5.5 times slower than Go itself which is a good result for the Wren interpreter.
<syntaxhighlight lang="
import "./fmt" for Fmt
import "io" for Stdout
|