Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created February 7, 2018 02:10
Show Gist options
  • Save tkihira/582263698b6c505cc1d6f2988a8cfc95 to your computer and use it in GitHub Desktop.
Save tkihira/582263698b6c505cc1d6f2988a8cfc95 to your computer and use it in GitHub Desktop.
var data = [1, 4, 5, 6, 7, 8];
//var data = [2, 3, 5, 7, 11, 13, 17];
//var data = [2, 3, 5, 10, 20];
var a = [];
var r = function(u) {
if(a.length == data.length - 1) {
if(u) return solve();
else return false;
}
if(!u) {
a.push("=");
r(true);
a.pop();
}
a.push("+"); r(u); a.pop();
a.push("-"); r(u); a.pop();
a.push("*"); r(u); a.pop();
};
var solve = function() {
var ret = ["" + data[0], ""];
var index = 0;
for(var i = 0; i < a.length; i++) {
if(a[i] == "=") { index++; ret[index] = data[i + 1]; }
else ret[index] += a[i] + data[i + 1];
}
if(eval(ret[0]) == eval(ret[1])) {
console.log(ret.join("="));
}
};
r(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment