Last active
February 13, 2018 12:19
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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