Sorry about deleting my comment, I totally broke my code
So I changed the .splice(j-1, 1, 0) to a direct array access and fixed the j offset, making it 2x faster
.splice(j-1, 1, 0)
j
function getLuckyNumbersTo(n, d=false) { const start = Date.now(); let lucky = new Array(n).fill(0).map((v,i) => i+1); if (d) console.log(lucky); for (let i = 2; i < lucky.length;) { for (let j = i; j <= lucky.length; j+=i) { lucky[j-1] = 0; } if (d) console.log(i, lucky.map(v => v)); lucky = lucky.filter(v => v !== 0); i = lucky.find(v => v >= i + 1); } const end = Date.now(); const duration = end - start; console.table({ size:n, length:lucky.length, time:duration }); console.log(lucky); return lucky; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Sorry about deleting my comment, I totally broke my codeSo I changed the
.splice(j-1, 1, 0)
to a direct array access and fixed thej
offset, making it 2x faster