Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- operators = {"/","*","+","-"};
- templates = {
- "%s %s %s %s %s %s %s",
- "(%s %s %s) %s %s %s %s",
- "%s %s (%s %s %s) %s %s",
- "%s %s %s %s (%s %s %s)",
- "(%s %s %s) %s (%s %s %s)",
- "(%s %s %s %s %s) %s %s",
- "%s %s (%s %s %s %s %s)",
- "((%s %s %s) %s %s) %s %s",
- "(%s %s (%s %s %s)) %s %s",
- "%s %s ((%s %s %s) %s %s)",
- "%s %s (%s %s (%s %s %s))",
- };
- function permutations(a,b,c,d)
- return {
- {a,b,c,d},
- {a,b,d,c},
- {a,c,b,d},
- {a,c,d,b},
- {a,d,c,b},
- {a,d,b,c},
- {b,a,c,d},
- {b,a,d,c},
- {b,c,a,d},
- {b,c,d,a},
- {b,d,c,a},
- {b,d,a,c},
- {c,b,a,d},
- {c,b,d,a},
- {c,a,b,d},
- {c,a,d,b},
- {c,d,a,b},
- {c,d,b,a},
- {d,b,c,a},
- {d,b,a,c},
- {d,c,b,a},
- {d,c,a,b},
- {d,a,c,b},
- {d,a,b,c},
- };
- end
- for a = 1, 6 do
- for b = a, 6 do
- for c = b, 6 do
- for d = c, 6 do
- local solutions = {};
- for _, permutation in ipairs(permutations(a,b,c,d)) do
- for _, template in ipairs(templates) do
- for _, op1 in ipairs(operators) do
- for _, op2 in ipairs(operators) do
- for _, op3 in ipairs(operators) do
- local calculation = "return " .. template:format(permutation[1],op1,permutation[2],op2,permutation[3],op3,permutation[4]);
- -- print(calculation .. " = " .. assert(load(calculation))());
- local calc = assert(load(calculation))();
- if calc == calc then --to check for NaN
- solutions[calc] = true;
- end
- calculation = "return " .. template:format(permutation[1],op1,permutation[2],op2,permutation[3],"+",0);
- -- print(calculation .. " = " .. assert(load(calculation))());
- local calc = assert(load(calculation))();
- if calc == calc then --to check for NaN
- solutions[calc] = true;
- end
- calculation = "return " .. template:format(permutation[1],op1,permutation[2],"+",0,"+",0);
- -- print(calculation .. " = " .. assert(load(calculation))());
- local calc = assert(load(calculation))();
- if calc == calc then --to check for NaN
- solutions[calc] = true;
- end
- calculation = "return " .. template:format(permutation[1],"+",0,"+",0,"+",0);
- -- print(calculation .. " = " .. assert(load(calculation))());
- local calc = assert(load(calculation))();
- if calc == calc then --to check for NaN
- solutions[calc] = true;
- end
- end
- end
- end
- end
- end
- local i = 1;
- while true do
- if not solutions[i] then
- print("N(" .. a .. "," .. b .. "," .. c .. "," .. d .. ") = " .. i);
- break
- end
- i = i + 1;
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement