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!!!!
| # | User | Rating |
|---|---|---|
| 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 |
| # | User | Contrib. |
|---|---|---|
| 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!!!!
| Name |
|---|



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.
hey, can you please provide me it's python based solution!
it will be really helpful for me
This is the code I used to count the numbers. It runs in 0.1s in my laptop.
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)
ohh nahh,
i hate yt lectures, and i am very impatient to watch videos and prefer straight to point book more,
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.
yeah, i am facing lot of such issues at spoj, but in my country grade 1 local olympiad for national selection will happen right after 1 months, so i dont think i will get comfortable with c++ in that time
but is there a way to learn it in 1 month?
edit
wait really!, O(N^2) compared to O(N LOG N)!!!, by this logic i think i can almost never get tle at c++!!, please provide me a e-book or a website to learn c++
Umm, i think the best way to learn is AI. But, you need to control yourself + say him, that you need C++ for CP. I learned from CodeChef, but right now almost all courses need payment... RIP CodeChef :sob:.
At your level python is fine. Focus on learning the basics of problem solving, algorithms and data structures.
You can switch to C++ in the future, if you think python is holding you back.