(phixonline)-->
with javascript_semantics
function Mcnugget_number(sequence counts)
if gcd(counts)>1 then return "No maximum" end if
atom cmin = min(counts)
sequence meals = {}
sequence smin = {}
integer a = -1
while true do
a += 1
for b=0 to a do
for c=0 to b do
sequence s = {a, b, c}
for i=1 to factorial(3) do
sequence p = permute(i,s)
integer k = sum(sq_mul(p,counts))+1
-- atom k = sum(sq_mul(p,counts))+1
if k>length(meals) then meals &= repeat(0,k-length(meals)) end if
meals[k] = 1
end for
end for
end for
for i=1 to length(meals) do
if meals[i] then
if length(smin) and smin[$]+1=i-1 then
smin = append(smin,i-1)
if length(smin)=cmin then exit end if
else
smin = {i-1}
end if
end if
end for
if length(smin)=cmin then exit end if
end while
return sprintf("%d",iff(smin[1]?smin[1]-1:0))
end function
constant tests = {{6,9,20}, {6,7,20}, {1,3,20}, {10,5,18}, {5,17,44}, {2,4,6}, {3,6,15}}
for i=1 to length(tests) do
sequence ti = tests[i]
printf(1,"Maximum non-Mcnugget number using %V is: %s\n",{ti,Mcnugget_number(ti)})
end for