I don't know if this is what you we going for or not, but assuming we can only use each operator once (there's no rule specified for this)
Also relevant: Your rule about not using each one is irrelevant, any number you can make with less can be made with all of them, at least, up to the smallest number
local Numbers = {
{2,5,4,2},
{2,5,2,4},
{2,2,5,4},
{2,2,4,5},
{2,4,5,2},
{2,4,2,5},
{4,2,5,2},
{4,2,2,5},
{4,5,2,2},
{5,2,4,2},
{5,2,2,4},
{5,4,2,2}
}
local Operators = {
{'+','/','','-'},
{'+','/','-',''},
{'+','-','/',''},
{'+','-','','/'},
{'+','','/','-'},
{'+','','-','/'},
{'-','+','','/'},
{'-','+','/',''},
{'-','/','+',''},
{'-','/','','+'},
{'-','','+','/'},
{'-','','/','+'},
{'','+','/','-'},
{'','+','-','/'},
{'','-','+','/'},
{'','-','/','+'},
{'','/','+','-'},
{'','/','-','+'},
{'/','+','','-'},
{'/','+','-',''},
{'/','-','+',''},
{'/','-','','+'},
{'/','','+','-'},
{'/','','-','+'}
}
function BracketForm(eq, f)
if f == 0 then
return eq
elseif f == 1 then
return "("..eq:sub(1,3)..")"..eq:sub(4)
elseif f == 2 then
return eq:sub(1,4).."("..eq:sub(5)..")"
elseif f == 3 then
return eq:sub(1,2).."("..eq:sub(3,5)..")"..eq:sub(6)
elseif f == 4 then
return "("..eq:sub(1,5)..")"..eq:sub(6)
elseif f == 5 then
return eq:sub(1,2).."("..eq:sub(3)..")"
elseif f == 6 then
return "(("..eq:sub(1,3)..")"..eq:sub(4,5)..")"..eq:sub(6)
elseif f == 7 then
return eq:sub(1,2).."(("..eq:sub(3,5)..")"..eq:sub(6)..")"
end
end
local Found = {}
for i,v in pairs(Numbers) do
for ii,vv in pairs(Operators) do
local equation = v[1] .. vv[1] .. v[2] .. vv[2] .. v[3] .. vv[3] .. v[4]
for i = 0, 7 do
equation2 = BracketForm(equation, i)
local ans = load("return " .. equation2)()
Found[ans] = ans > 0 and ans%1 == 0 and not Found[ans] and equation2 or nil
end
end
end
local answers = {}
for i, v in pairs(Found) do
answers[#answers+1] = i
end
for i = 1,#answers do
local least = math.min(table.unpack(answers))
local eq = Found[least]
for ii = 1, #answers do
if answers[ii] == least then
table.remove(answers, ii)
end
end
print(eq .."="..least)
end