Created
May 27, 2024 23:36
-
-
Save kengonakajima/b207b69ae24f2787f7cc809876bab7f3 to your computer and use it in GitHub Desktop.
beltmatic solver tool
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
function generateExpressions(target, count, maxNum) { | |
const operators = ['+', '-', '*', '/']; | |
let n=0; | |
while (n < count) { | |
const nums = []; | |
const ops = []; | |
let numCount = Math.floor(Math.random() * 4) + 2; | |
for (let i = 0; i < numCount; i++) { | |
nums.push(Math.floor(Math.random() * maxNum) + 1); | |
} | |
for (let i = 0; i < numCount - 1; i++) { | |
ops.push(operators[Math.floor(Math.random() * 4)]); | |
} | |
const expression = nums.reduce((acc, num, index) => { | |
return acc + num + (ops[index] || ''); | |
}, ''); | |
if (eval(expression) === target) { | |
console.log(target,"=",expression); | |
n++; | |
} | |
} | |
} | |
// | |
const args = process.argv.slice(2); | |
const target = parseInt(args[0]); | |
const count = parseInt(args[1]) || 10; | |
const maxNum = parseInt(args[2]) || 100; | |
if (isNaN(target)) { | |
console.error('please specify a number'); | |
process.exit(1); | |
} | |
generateExpressions(target, count, maxNum); |
Author
kengonakajima
commented
May 27, 2024
•
solve.js 1123 10 11
1123 = 3+7*10*8*2
1123 = 3*7*9*6-11
1123 = 4*7*5*8+3
1123 = 3+4*8*5*7
1123 = 3+7*10*8*2
1123 = 3+4*5*7*8
1123 = 3+4*10*7*4
1123 = 7*4*5*8+3
1123 = 3+4*10*7*4
1123 = 7*9*2*9-11
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment