Last active
February 2, 2022 09:19
-
-
Save Dobby233Liu/9d167e724fddf350c833bc28c19482b2 to your computer and use it in GitHub Desktop.
GWCB2TNA2TS - silly JS code
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
// Get What Can Be Crafted By Thou Numbers And Also Their Sum (GWCB2TNA2TS) | |
// WTFPL | |
// but, opitmiz and credit suggested... can you opitmiz it better than my better anyway? (optmiz my better one, not others!) tell me by forking, coding and commenting! | |
// beautified using https://beautifier.io | |
function gwcb2tna2ts(im = 4, jm = 4, km = 4) | |
{ // https://blog.csdn.net/cherrydreamsover/article/details/79750556 port | |
im++; | |
jm++; | |
km++; | |
var i = 0; // 百位 | |
var j = 0; // 十位 | |
var k = 0; // 个位 | |
var o = []; // numbers | |
var h = 0; // sum | |
for(i = 1; i < im; i++) | |
{ | |
for(j = 1; j < jm; j++) | |
{ | |
for(k = 1; k < km; k++) | |
{ | |
if((i != j) && (i != k) && (j != k)) // 判断不相等 | |
{ | |
o.push(i * 100 + j * 10 + k); | |
} | |
} | |
} | |
} | |
return { | |
nums: o, | |
sum: (() => { | |
o.forEach((e) => h += e); | |
return (true) | |
})() && h | |
}; | |
} | |
function gwcb2tna2ts_4N(tm = 9, im = 9, jm = 9, km = 9) | |
{ // https://blog.csdn.net/cherrydreamsover/article/details/79750556 port, supports 4params | |
tm++; | |
im++; | |
jm++; | |
km++; | |
var t = 0; // thoustands | |
var i = 0; // 百位 | |
var j = 0; // 十位 | |
var k = 0; // 个位 | |
var o = []; // numbers | |
var h = 0; // sum | |
for(t = 1; t < tm; t++) { | |
for(i = 1; i < im; i++) | |
{ | |
for(j = 1; j < jm; j++) | |
{ | |
for(k = 1; k < km; k++) | |
{ | |
if((t != i) && (t != j) && (t != k) && (i != j) && (i != k) && (j != k)) // 判断不相等 | |
{ | |
o.push(t * 1000 + i * 100 + j * 10 + k); | |
} | |
} | |
} | |
} | |
} | |
return { | |
nums: o, | |
sum: (() => { | |
o.forEach((e) => h += e); | |
return (true) | |
})() && h | |
}; | |
} | |
function gwcb2tna2tsBetter(narr = [0, 1, 5, 8], chars = 4) { | |
for (i of narr) { | |
if (isNaN(i) || !Number.isInteger(i)) { | |
throw new Error("we want only integers in the narr, and narr must be just an array"); | |
} | |
} | |
var biggest = +(narr.sort((a, b) => b - a).slice(0, chars).join('')); | |
var smallStart = +(narr.sort((a, b) => a - b).slice(0, chars).join('')); | |
var o = []; | |
for(var i = smallStart; i <= biggest; i++) { | |
var right = true; | |
var chk = i.toString(); | |
for (i2 of chk) | |
{ | |
if (!narr.includes(parseInt(i2))) { | |
right = false; | |
break; | |
} | |
} | |
if(right) o.push(i); | |
} | |
var sum = 0; | |
for (i of o) { | |
sum += i; | |
} | |
return { | |
nums: o, | |
sum: sum | |
}; | |
} |
outdated
Inefficienticy found when playing with the fluid version: if the array length >= 4, and there is 3+ numbers that is > 9 in the array, the script will become unresponsive for a long or short time.
Testcase:
/**> **/gwcb2tna2tsBetter([9,10,10,10])
// ... ... few >= 10 millseconds, no output ... ...
/**< **/{nums: Array(20538), sum: 11944296978}
No too much speed incremental even when I remove the sum part.
edit: due of:
right |= stringisc.indexOf(narr[i3]);
this hack is stupid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
outdated
note: fluid=better. I use Date.now and DevTools
Benchmark: