I somehow managed to improve its execution by a considerable margin
I somehow managed to improve its execution by a considerable margin (60000 now computes in 27 seconds instead of over 4 minutes HURRAH!) but I still can't get it under 12 seconds sad face
for this new function I removed sort from loop
but instead I had to double the number of iterations and it seems that when n > 6000 I have to increase them even more in order to get correct results sad face
I could use some help to decrease the iterations and improve even further execution time
for those who are wondering what exercise this is:
for this new function I removed sort from loop
but instead I had to double the number of iterations and it seems that when n > 6000 I have to increase them even more in order to get correct results sad face
I could use some help to decrease the iterations and improve even further execution time
for those who are wondering what exercise this is:
function dblLinear(n) {
let sequenceArr = [1],
i = n+n,
buffArr = [],
x = 1,
y,z;
while (i--) {
y = 2 * x + 1;
if (sequenceArr.indexOf(y) === -1) sequenceArr.push(y);
z = 3 * x + 1;
if (sequenceArr.indexOf(z) === -1) sequenceArr.push(z);
if (!buffArr.length) buffArr = []; buffArr.push(y,z);
x = buffArr.shift();
}
// l(JSON.stringify(sequenceArr.sort((a,b)=> a-b)));
return sequenceArr.sort((a,b) => a-b)[n];
}
console.log(dblLinear(10));// 22
console.log(dblLinear(20));// 57
console.log(dblLinear(30));// 91
console.log(dblLinear(50));// 175
console.log(dblLinear(100));// 447
console.log(dblLinear(500)); // 3355
console.log(dblLinear(1000)); // 8488
console.log(dblLinear(2000)); // 19773
console.log(dblLinear(6000)); // 80914
console.log(dblLinear(60000)); // LOL
No any search results
You already invited:
1 Answers
Nigel
Upvotes from:
function dblLinear(n) {
// your code
let u = [1];
let y_index = 0;
let z_index = 0;
let gen_y = () => u[y_index++] * 2 + 1;
let gen_z = () => u[z_index++] * 3 + 1;
let y = gen_y()
let z = gen_z()
while (u.length <= n) {
if (y < z) {
u.push(y)
y = gen_y();
} else if (z < y) {
u.push(z);
z = gen_z();
}
else {
u.push(y);
y = gen_y();
z = gen_z();
}
}
return u[n];
}