Skip to main content
deleted 38 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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:

  1. set zero in their place and access the list by index
  2. 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}\$.

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, this is most of the numbers... Moreover your remove item by value, that mean 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 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 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, just sum them.

if you want to check if the number n is prime, you don't need to check division for all the numbers. Check only from 2 to sqrt(n).

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, which is most of the numbers.

Moreover, your remove items by value, which 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, you

  1. set zero in their place and access the list by index
  2. 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 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; just sum them.

if you want to check if the number \$n\$ is prime, you don't need to check division for all the numbers. Check only from 2 to \$\sqrt{n}\$.

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, this is most of the numbers... Moreover your remove item by value, that mean that each time you remove it, a searching by value is performed on the list. making it even slower..

You can aviodavoid this if isteadinstead of removing the non-primes just set zero in thiertheir place, and access the list by index. Then build a list for collecting the found primes

But I would suggest a completlycompletely different approach:

Instead of starting with list of all numbers all = list(range(2, 2000000)), and then removing the non-primes. It is better just start with empty list and adding the number if it is prime. Or if you need only the sum, dontdon't save the primes at all, just sum them.

if you want to check if the number n is prime, you dontdon't need to check divitiondivision for all the numbers. Check only from 2 to sqrt(n)sqrt(n).

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, this is most of the numbers... Moreover your remove item by value, that mean that each time you remove it, a searching by value is performed on the list. making it even slower..

You can aviod this if istead of removing the non-primes just set zero in thier place, and access the list by index. Then build a list for collecting the found primes

But I would suggest a completly different approach:

Instead of starting with list of all numbers all = list(range(2, 2000000)), and then removing the non-primes. It is better just start with empty list and adding the number if it is prime. Or if you need only the sum, dont save the primes at all, just sum them.

if you want to check if the number n is prime, you dont need to check divition for all the numbers. Check only from 2 to sqrt(n).

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, this is most of the numbers... Moreover your remove item by value, that mean 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 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 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, just sum them.

if you want to check if the number n is prime, you don't need to check division for all the numbers. Check only from 2 to sqrt(n).

Source Link

Most of the time the line all.remove(x) is running, and it is executed for all non-prime numbers, this is most of the numbers... Moreover your remove item by value, that mean that each time you remove it, a searching by value is performed on the list. making it even slower..

You can aviod this if istead of removing the non-primes just set zero in thier place, and access the list by index. Then build a list for collecting the found primes

But I would suggest a completly different approach:

Instead of starting with list of all numbers all = list(range(2, 2000000)), and then removing the non-primes. It is better just start with empty list and adding the number if it is prime. Or if you need only the sum, dont save the primes at all, just sum them.

if you want to check if the number n is prime, you dont need to check divition for all the numbers. Check only from 2 to sqrt(n).