{{ message }}

Instantly share code, notes, and snippets.

# Dobby233Liu/gwcb2tna2ts.js

Last active Mar 27, 2021
GWCB2TNA2TS - silly JS code
 // 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]) { var boarr = narr.sort((a, b) => b - a); var biggest = +(boarr.join('')); if(isNaN(biggest)) { console.error("biggest number guessing failed. invaild narr? narr:", narr, ", guessed biggest:", biggest, ", boarr:", boarr); throw new Error("biggest number guessing failed. invaild narr?") } //var smallNumberCrafted = Number(boarr.join('')); var o = []; for(var i = 0; i < (biggest + 1); i++) { //var stringisc = i.toString(); //var right = 0; //for(i3 in narr) right |= stringisc.indexOf(narr[i3]); var right = true; var chkarr = i.toString().split(""); for (i2 in narr) { if (!chkarr.indexOf(narr[i2])) { right = false; break; } } if(right) o.push(i); } return { nums: o, sum: (() => { h = 0; o.forEach((e) => h += e); return (h) })() }; }

### Dobby233Liu commented Mar 28, 2020 • edited

 outdated note: fluid=better. I use Date.now and DevTools Benchmark: defaults 3-digit version uses 0ms 4-digit version uses 2ms fluid version uses 36ms 3-digit ([2,4,6]) original uses 0ms fluid version uses 2ms 4-digit ([2,4,6,8]) original 4-digit version uses 0ms fluid version uses 23ms var c=Date.now() gwcb2tna2ts() var cd = Date.now()-c var c2=Date.now() gwcb2tna2ts_4N() var c2d = Date.now()-c2 var c3=Date.now() gwcb2tna2tsBetter() var c3d = Date.now()-c3 console.log(cd,c2d,c3d) var c=Date.now() gwcb2tna2ts(2,4,6) var cd = Date.now()-c var c3=Date.now() gwcb2tna2tsBetter([2,4,6]) var c3d = Date.now()-c3 console.log(cd,c3d) var c2=Date.now() gwcb2tna2ts_4N(2,4,6,8) var c2d = Date.now()-c2 var c3=Date.now() gwcb2tna2tsBetter([2,4,6,8]) var c3d = Date.now()-c3 console.log(c2d,c3d)

### Dobby233Liu commented Mar 28, 2020 • edited

 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