Possible Duplicates:
Fastest way to list all primes below N in python
Checking if a number is a prime number in Python
I am working on Project Euler Problem 10, which states as follows:
Find the sum of all the primes below two million.
Here's my program:
numbers = []
sum = 0
range_number = 2000000
#Appends all numbers in range
for i in range(2, range_number):
numbers.append(i)
#i is every entry in numbers, n is the multiples of numbers[i] starting at one
#value of numbers[i] after i. This is the Sieve of Eratosthenes.
for i in range(0, len(numbers)-1):
if numbers[i] != None:
for n in range(i + numbers[i], len(numbers)-1, numbers[i]):
numbers[n] = None
#Adds all the numbers that are not None
for i in numbers:
if i != None:
sum += i
print(sum)
My program changes all multiples of every number below the range to None, which should eliminate all composites and leave only primes.
When I plug in a simple number for range_number like 10, I get the wrong answer. Instead of just posting your own program, please tell me where I went wrong. Other posts mentioned using the square root, but I didn't really get that.
Thanks.