https://www.spoj.com/problems/TDPRIMES/ https://www.spoj.com/problems/HS08PAUL/
it is too slow in python with sieve
if someone knows a way to optimize it or a trick then please tell me!!!!
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 146 |
| 3 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
https://www.spoj.com/problems/TDPRIMES/ https://www.spoj.com/problems/HS08PAUL/
it is too slow in python with sieve
if someone knows a way to optimize it or a trick then please tell me!!!!
| Название |
|---|



In Python, if you try to check every number one by one to see if it’s prime, it’ll take forever. You have to use a trick called the Sieve of Eratosthenes. Think of it like this: you lay out all numbers from 2 to 100 million. You start at 2 and cross out every 2nd number (4, 6, 8...). Then you go to 3 and cross out every 3rd number. By the time you’re done, only the primes are left standing.
actually i did tried sieve,
but it was not working!!, it still took a hell lot of time!
to me, sieve always gives TLE
like there is this very simple problem
i used python template of sieve and implemented it here https://www.spoj.com/problems/VECTAR8/
and even it gave tle!
The problem is not the sieve, the problem is your algorithm. There are only 671 of these numbers in the range. You can generate them all quickly with backtracking and a sieve.
maybe use bytearray in python, generally python is too slow to run for 1e8 sieve tho
hey i have tried every single thing possible, bytearray took a lot less time , but still thrice the limit and TLE
that's why in competitive programming you should use c++ and shouldn't use python
yeah but i think c++ is wayy too diffcult and complex,
i find python easier and simpler+faster to code in, it gives me advantage when i have to solve a problem fast, and took Pajenegod as a idol
btw if you have any resource to learn c++ ASAP then please provide it , i will start learning c++ now
there are many resources like codewithharry,apna college,college wallah(from raghav sir)
I know that typing in Python better, but trust me:
It's better to learn C++! If you really want to go to the EJOI/IOI/ICPC, you must learn C++:
Time Limit Exceeded (TLE):
In CP, TLE is your best friend if you use Python. C++ gives you a massive 'safety buffer' because of its raw execution speed.
The STL Advantage:
C++ STL is designed for performance and efficiency. While you're looking for a library to balance your tree, I've already solved the problem using
std::mapandstd::set.Efficiency:
A $$$O(N^2)$$$ solution in C++ sometimes passes where a $$$O(N \log N)$$$ in Python fails. That's how much of a gap we're talking about.
Memory Overhead:
Python’s memory management is too bloated for tight constraints. C++ is lean and mean.