Skip to content

Instantly share code, notes, and snippets.

@nektro
Last active February 13, 2018 12:19
Show Gist options
  • Select an option

  • Save nektro/44e87fda59d36335f68231cc9760b035 to your computer and use it in GitHub Desktop.

Select an option

Save nektro/44e87fda59d36335f68231cc9760b035 to your computer and use it in GitHub Desktop.
Find the [Lucky Numbers](https://en.wikipedia.org/wiki/Lucky_number) up to a certain `n`
function getLuckNumbersTo(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.splice(j-1, 1, 0);
}
lucky = lucky.filter(v => v !== 0);
if (d) console.log(i, lucky);
i = lucky.find(v => v >= i + 1);
}
const end = Date.now();
const duration = end - start;
console.table({ size:n, length:lucky.length, time:duration });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment