Created
November 27, 2020 18:40
-
-
Save justinbmeyer/dc11015d89a44f5a4b0fd949fe677214 to your computer and use it in GitHub Desktop.
how many numbers can you create with five 2s?
This file contains 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
<script> | |
function fac(num) { | |
if(!Number.isInteger(num)) { | |
return NaN; | |
} | |
if(num > 10) { | |
return Infinity; | |
} | |
if (num === 0 || num === 1) | |
return 1; | |
for (var i = num - 1; i >= 1; i--) { | |
num *= i; | |
} | |
return num; | |
} | |
function bnot(num) { | |
return ~num | |
} | |
function add(a, b){ return a + b } | |
add.print = (a, b) => "("+a+"+"+b+")"; | |
function sub(a, b){ return a - b } | |
sub.print = (a, b) => "("+a+"-"+b+")"; | |
function mul(a, b){ return a * b }; | |
mul.print = (a, b) => "("+a+"*"+b+")"; | |
function div(a, b){ return a / b }; | |
div.print = (a, b) => "("+a+"/"+b+")"; | |
function band(a, b) { return a & b } | |
band.print = (a, b) => "("+a+"&"+b+")"; | |
function mod(a, b) { return a % b }; | |
mod.print = (a, b) => "("+a+"%"+b+")"; | |
function pow(a, b) { return Math.pow(a, b) } | |
pow.print = (a, b) => "("+a + "^"+ b+")" | |
var operations = [ | |
add,sub,mul, div, | |
// power | |
pow, | |
band, | |
mod | |
]; | |
var unaryOperations = [ | |
{ | |
test: (value)=> Number.isInteger(value) && value < 5, | |
operation: fac, | |
print: (child) => "(!"+child+")" | |
}, | |
{ | |
test: () => true, | |
operation: bnot, | |
print: (child) => "(~"+child+")" | |
} | |
]; | |
function unaryAnswers(result) { | |
return unaryOperations.filter(op => op.test(result.value)) | |
.map( (op) => { | |
return { | |
value: op.operation(result.value), | |
equation: op.print(result.equation) | |
} | |
}) | |
} | |
var cache = {}; | |
function answers(roots, parent){ | |
if(roots.length === 1) { | |
return [{ | |
value: roots[0], | |
equation: ""+roots[0] | |
}] | |
} | |
if(cache[roots.join(",")]) { | |
return cache[roots.join(",")]; | |
} | |
var allResults =[]; | |
if(roots.length === 2) { | |
for( let operation of operations) { | |
var equation = operation.print( roots[0], roots[1] ); | |
var value = operation(roots[0], roots[1]); | |
let result = { | |
value: value, | |
equation: equation | |
}; | |
allResults.push(result); | |
allResults.push(...unaryAnswers(result)); | |
// should we try factorial? | |
} | |
} else { | |
console.log("calculating for", roots.length); | |
for(let i = 1; i < roots.length; i++) { | |
console.log(parent, roots.slice(0,i).length, roots.slice(i).length) | |
var leftResults = answers( roots.slice(0,i), roots.length); | |
var rightResults = answers( roots.slice(i), roots.length); | |
// try every operation on left and right given every result | |
for(let leftResult of leftResults) { | |
for( let rightResult of rightResults) { | |
for( let operation of operations) { | |
var equation = operation.print( | |
leftResult.equation , rightResult.equation); | |
var value = operation(leftResult.value, rightResult.value); | |
let result = { | |
value: value, | |
equation: equation | |
}; | |
allResults.push(result); | |
allResults.push(...unaryAnswers(result)); | |
} | |
} | |
} | |
} | |
} | |
cache[roots.join(",")] = allResults; | |
return allResults; | |
} | |
var grouped = {}; | |
answers([2,2,2,2, 2] ).forEach(function(answer){ | |
if(!grouped[answer.value]) { | |
grouped[answer.value] = []; | |
} | |
grouped[answer.value].push(answer); | |
}) | |
answers([22,2,2,2] ).forEach(function(answer){ | |
if(!grouped[answer.value]) { | |
grouped[answer.value] = []; | |
} | |
grouped[answer.value].push(answer); | |
}) | |
answers([2,2,2,22] ).forEach(function(answer){ | |
if(!grouped[answer.value]) { | |
grouped[answer.value] = []; | |
} | |
grouped[answer.value].push(answer); | |
}) | |
setTimeout(function(){ | |
answers([22,2,22] ).forEach(function(answer){ | |
if(!grouped[answer.value]) { | |
grouped[answer.value] = []; | |
} | |
grouped[answer.value].push(answer); | |
}) | |
answers([2, 22,22] ).forEach(function(answer){ | |
if(!grouped[answer.value]) { | |
grouped[answer.value] = []; | |
} | |
grouped[answer.value].push(answer); | |
}) | |
console.log(grouped); | |
for(var i = 0; i < 200; i++) { | |
if(!grouped[i]){ | |
console.log(i) | |
} | |
} | |
},100) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment