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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment