optimization attemp

This commit is contained in:
2017-08-16 15:34:03 +02:00
parent 4e87ae8bb6
commit 5333ca0870

View File

@@ -4,30 +4,31 @@
function findPosition(num) { function findPosition(num) {
let next = (function() { let next = (function() {
let n = 1; let n = 1;
let cnt = 0; let pos = 0;
let buf = []; let buf = [];
return (len) => { return (len, cs) => {
while (buf.length < len) { let cl = buf.join('').length;
buf.push(...(''+(n++)).split('')); while (cl < len + cs) {
let sn = (n++)+'';
buf.push(sn);
cl += sn.length;
} }
let res = buf.join('').substr(0, len); let res = buf.join('');
buf.shift(); buf = [res.substr(cs)];
return [res, cnt++]; return [res, (pos += cs) - cs];
}; };
}()); }());
while (1) { while (1) {
let n = next(num.length); let n = next(num.length, 1e4);
// console.log(n[0]); // console.log(n[0]);
if (n[0] === num) { let idx = n[0].indexOf(num);
return n[1]; if (0 <= idx) {
} return n[1] + idx;
if (1e8 < n[1]) {
console.log(num);
break;
} }
} }
} }
console.log(findPosition('1000000071')); console.log(findPosition('1000000071'));
// console.log(findPosition('1001'));