Most of the time the line all.remove(x)
is running, and it is executed for all non-prime numbers, thiswhich is most of the numbers...
Moreover
Moreover, your remove itemitems by value, that meanwhich means that each time you remove it, a searching by value is performed on the list., making it even slower..
You can avoid this if, instead of removing the non-primes just set zero in their place, and access the list by index. Then build a list for collecting the found primesyou
But I would suggest a completely different approach:
- set zero in their place and access the list by index
- build a list for collecting the identified primes
But I would suggest a completely different approach:
Instead of starting with list of all numbers all = list(range(2, 2000000))
, and then removing the non-primes. It, it is better just start with empty list and adding the number if it is prime. Or if you need only the sum, don't save the primes at all,all; just sum them.
if you want to check if the number n\$n\$ is prime, you don't need to check division for all the numbers. Check only from 2 to sqrt(n)\$\sqrt{n}\$.