Created
June 25, 2025 16:01
-
-
Save rhogeranacleto/d7a6ac5a56ae8dca3023774c10484d84 to your computer and use it in GitHub Desktop.
Numeric core
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
| const operators = [ | |
| ["-", "*", "/"], | |
| ["-", "/", "*"], | |
| ["*", "-", "/"], | |
| ["*", "/", "-"], | |
| ["/", "-", "*"], | |
| ["/", "*", "-"], | |
| ]; | |
| const operate = (n1, n2, operation) => { | |
| switch (operation) { | |
| case "-": | |
| return n1 - n2; | |
| case "*": | |
| return n1 * n2; | |
| case "/": | |
| return n1 / n2; | |
| } | |
| }; | |
| const getCore = (n) => { | |
| const nString = n.toString(); | |
| if (nString.length < 4) throw new Error("Can less then 1000"); | |
| const algarism = nString.split(""); | |
| const length = algarism.length; | |
| const groupsOf4 = []; | |
| for (let i = 1; i <= length - 3; i++) { | |
| for (let j = i + 1; j <= length - 2; j++) { | |
| for (let k = j + 1; k <= length - 1; k++) { | |
| const group1 = algarism.slice(0, i); | |
| const group2 = algarism.slice(i, j); | |
| const group3 = algarism.slice(j, k); | |
| const group4 = algarism.slice(k); | |
| groupsOf4.push([group1.join(""), group2.join(""), group3.join(""), group4.join("")]); | |
| } | |
| } | |
| } | |
| const result = {}; | |
| for (const group of groupsOf4) { | |
| const groupNumber = group.map((i) => Number(i)); | |
| for (const operation of operators) { | |
| const calculus = `${group[0]} ${operation[0]} ${group[1]} ${operation[1]} ${group[2]} ${operation[2]} ${group[3]}`; | |
| let total = operate(groupNumber[0], groupNumber[1], operation[0]); | |
| total = operate(total, groupNumber[2], operation[1]); | |
| total = operate(total, groupNumber[3], operation[2]); | |
| result[calculus] = total; | |
| } | |
| } | |
| const min = Object.entries(result) | |
| .filter(([c, v]) => Number.isInteger(v) && v > 0) | |
| .reduce((previous, current) => { | |
| if (!previous) return current; | |
| if (current[1] < previous[1]) return current; | |
| return previous; | |
| }); | |
| console.log(`The following calculus ${min[0]} result in the core number ${min[1]}`); | |
| return result; | |
| }; | |
| console.log(getCore(125620)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment