You are given 6 numbers: 5 digits [0-9] and a target number. Your goal is to intersperse operators between the digits to get as close as you can to the target. You have to use each digit exactly once, and can use the following operators as many times as you want: + - * / () ^ sqrt sin cos tan
. For example, if I'm given 8 2 4 7 2 65
I can output 82-(2*7)-4
. This evaluates to 64, thus giving me a score of 1 since I was 1 away from the target. Note: You can not put a decimal point between digits.
I am using the code from this StackOverflow answer to evaluate the mathematical expressions. At the bottom of this question there are programs you can use to test it out.
Chaining Functions (Update!)
@mdahmoune has revealed a new level of complexity to this challenge. As such, I'm adding a new feature: chaining unary functions. This works on sin, cos, tan, and sqrt. Now instead of writing sin(sin(sin(sin(10))))
, you can write sin_4(10)
. Try it out in the evaluator!
Input
200 line-separated test cases of 5 digits and a target number that are space-separated. You can use the program at the bottom of the question to make sample test cases, but I will have my own test cases for official scoring. The test cases are broken up into 5 sections of 40 tests with the following ranges for the target number:
- Section 1: [0,1] (to 5 decimal points)
- Section 2: [0,10] (to 4 decimal points)
- Section 3: [0,1000] (to 3 decimal points)
- Section 4: [0,106] (to 1 decimal point)
- Section 5: [0,109] (to 0 decimal points)
Output
200 line separated mathematical expressions. For example, if the test case is 5 6 7 8 9 25.807
, a possible output could be 78-59+6
Scoring
The goal each round is to get closer to the target number than the other competing programs. I'm going to use Mario Kart 8 scoring, which is: 1st: 15 2nd: 12 3rd: 10 4th: 9 5th: 8 6th: 7 7th: 6 8th: 5 9th: 4 10th: 3 11th: 2 12th: 1 13th+: 0
. If multiple answers get the same exact score, the points are split evenly, rounded to the nearest int. For example, if the programs in 5th-8th place are tied, they each get (8+7+6+5)/4 = 6.5 => 7 points that round. At the end of 200 rounds, the program that got the most points wins. If two programs have the same number of points at the end, the tie-breaker is the program that finished running faster.
Rules
- You can only use one of the languages commonly pre-installed on Mac like C, C++, Java, PhP, Perl, Python (2 or 3), Ruby, and Swift. If you have a language you want to use with a compiler/interpreter that is a relatively small download I may add it. You can also use a language with an online interpreter, but that will not run as fast.
- Specify in your answer if you want trig functions to be calculated in degrees or radians.
- Your program must output its solutions to all 200 test cases (to a file or STDOUT) within 60 seconds on my Mac.
- Randomness must be seeded.
- Your total output for all test cases can't be more than 1 MB.
- If you have improved your solution and would like to be re-scored, add Re-Score at the top of your answer in bold.
Programs
(change the "deg" argument to "rad" if you want radians)
- Test out evaluator
- Score your program's output for test cases
- Generate test cases:
document.getElementById("but").onclick = gen;
var checks = document.getElementById("checks");
for(var i = 1;i<=6;i++) {
var val = i<6 ? i : "All";
var l = document.createElement("label");
l.for = "check" + val;
l.innerText = " "+val+" ";
checks.appendChild(l);
var check = document.createElement("input");
check.type = "checkBox";
check.id = "check"+val;
if(val == "All") {
check.onchange = function() {
if(this.checked == true) {
for(var i = 0;i<5;i++) {
this.parentNode.elements[i].checked = true;
}
}
};
}
else {
check.onchange = function() {
document.getElementById("checkAll").checked = false;
}
}
checks.appendChild(check);
}
function gen() {
var tests = [];
var boxes = checks.elements;
if(boxes[0].checked)genTests(tests,1,5,40);
if(boxes[1].checked)genTests(tests,10,4,40);
if(boxes[2].checked)genTests(tests,1000,3,40);
if(boxes[3].checked)genTests(tests,1e6,1,40);
if(boxes[4].checked)genTests(tests,1e9,0,40);
document.getElementById("box").value = tests.join("\n");
}
function genTests(testArray,tMax,tDec,n) {
for(var i = 0;i<n;i++) {
testArray.push(genNums(tMax,tDec).join(" "));
}
}
function genNums(tMax,tDec) {
var nums = genDigits();
nums.push(genTarget(tMax,tDec));
return nums;
}
function genTarget(tMax,tDec) {
return genRand(tMax,tDec);
}
function genRand(limit,decimals) {
var r = Math.random()*limit;
return r.toFixed(decimals);
}
function genDigits() {
var digits = [];
for(var i = 0;i<5;i++) {
digits.push(Math.floor(Math.random()*10));
}
return digits;
}
textarea {
font-size: 14pt;
font-family: "Courier New", "Lucida Console", monospace;
}
div {
text-align: center;
}
<div>
<label for="checks">Sections: </label><form id="checks"></form>
<input type="button" id="but" value="Generate Test Cases" /><br/><textarea id="box" cols=20 rows=15></textarea>
</div>
Leaderboard
Section scores (# of wins):
- [0-1] user202729: 40, mdahmoune: 0
- [0-10] user202729: 40, mdahmoune: 0
- [0-1000] user202729: 39, mdahmoune: 1
- [0-106] user202729: 33, mdahmoune: 7
- [0-109] user202729:0, mdahmoune: 40
Related: Generate a valid equation using user-specified numbers
cos(0)/sin(0^0)/sin(0^0)
. \$\endgroup\$