Created
October 22, 2017 01:59
-
-
Save dzt/eaac240164e60fdcebdda4fe2ad78133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var math = require('mathjs'); | |
/* | |
You One 1s and Four 7s, how do you make these numbers equal 100 with any operation. | |
*/ | |
// dictionary to create combinations from | |
var dictionary = ['+', '-', '/', '*']; | |
// all combinations with 4 letters | |
var comb = combination(dictionary, 5); | |
var result = null; | |
for (var i = 0; i < comb.length; i++) { | |
var expression = math.eval(`1 ${comb[i].charAt(0)} 7 ${comb[i].charAt(1)} 7 ${comb[i].charAt(2)} 7 ${comb[i].charAt(3)} 7`); | |
console.log(`1 ${comb[i].charAt(0)} 7 ${comb[i].charAt(1)} 7 ${comb[i].charAt(2)} 7 ${comb[i].charAt(3)} 7 = ${expression}`); | |
if (expression == 100 || expression == "100") { | |
result = `1 ${comb[i].charAt(0)} 7 ${comb[i].charAt(1)} 7 ${comb[i].charAt(2)} 7 ${comb[i].charAt(3)} 7`; | |
} | |
} | |
if (result) { | |
console.log("Result Found: " + result); | |
} else { | |
console.log("No Result was found"); | |
} | |
function combination(dictionary, length, depth, input, output) { | |
if (!depth) { | |
output = []; | |
input = ''; | |
depth = 0; | |
} | |
dictionary.forEach(function(element) { | |
if (depth == length - 1) { | |
output.push(input + element); | |
} else { | |
combination(dictionary, length, depth + 1, input + element, output); | |
} | |
}); | |
if (!depth) | |
return output; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment