I have tried sieve and segmented sieve too but they are slow. Can anyone provide code to print primes till 10^9 FAST?
Python code would be a great help.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I have tried sieve and segmented sieve too but they are slow. Can anyone provide code to print primes till 10^9 FAST?
Python code would be a great help.
Название |
---|
AFAIK, just using sieve to count prime upto 1e9 is already 600ms for fasted code I known (run on GNU G++17 7.3.0 CF 23/9/2021). Taking all primes $$$\leq 10^9$$$ is already 4-5 seconds ($$$50,847,534$$$ numbers).
Assuming your " FAST " mean 1 second then I would like to say it must be very heavily micro-optimized for each operation to do so.
But enumerate through all prime $$$\leq 10^9$$$ under 1 second is possible if you manage to maintain the prime array for each range
600ms seems quite fast if the sieve is actually generating all of the primes in that range and not using number theory magic to count them in $$$o(n)$$$. Care to share? I tried writing a somewhat optimized segmented sieve in Haskell in response to this blog's now-deleted predecessor, getting generation down to about 1.7 seconds and generation+printing down to about 2.8 seconds. My code seemed to get mangled when I tried to insert it into this comment (probably due its use of
$
interacting poorly with MathJax), so here it as a submission link: 141417208. The quoted times above are from Custom Invocation with input1000000000 240000 1200
.I estimate the printing alone would take about 2 seconds, and I'm not aware of any integer output template in any language that's appreciably faster than the one I use in my Haskell code, so 1 second for generation+printing might be infeasible. But obviously an un-fused lazy singly-linked list is not a very efficient data structure to hold 50 million
Int
s in (it would be over 2GB if it existed in memory all at once...), and I don't think GHC is very good at optimizing this kind of array-heavy code, so maybe it's possible to actually generate the primes a good deal faster than I do.Well I just use my own modified version from Bitwise Segment Sieve from prime sieve github (which some bitwise wheel sieve precalculation) that specifically sieve for int $$$\leq 10^9$$$. If just to count number then it is fast but if to collect all prime numbers then it is much slower than Non-bitwise one.
The code 23 and 24 are lost during the covid but I think it still being saved somewhere in my old computer. Yet I will try to remake a new one if possible.
Lost the original files.
<spoiler summary=24.Improved Erastothenes Segment Bitwise Sieve">
Lost the original files.
You can just use the normal sieve to print all she primes upto 1e9 which takes about 5 seconds than copy the primes (which are about 3000) and hard-code them in a vector. Then you don't have to calculate them in the actual submission
thanks worked for me