Reputation: 447
I'm a newbie attempting ProjectEuler problems.
Problem #10 is as follows: Find the sum of all the primes below two million.
My code is as follows
a=1
s=0
p=[] #just wanna see the primes for myself so creating a list for that
while a<2000000: #while value of primes is less than 2million
if all(a%i!=0 for i in range(3,int(a**0.5)+1)): #checks if the number is prime
s=s+a #add primes to the sum
p.append(a) #add the primes to their list
a=a+2 #so that we only test odd numbers
print(s)
print(p)
So the problem is, the number 1 pops up in the list, and 2 doesn't show up. All other prime numbers get included. I tried many tweaks to find the sum and list of primes below 10 but the problem persists. What am I doing wrong?
Upvotes: 0
Views: 274
Reputation: 2073
Start with a=5, ignoring the first 2 primes 2 and 3.
and s = 5 (adding up the first 2 primes, 2 and 3)
Loop from 5 to 2 million.
The a = a+2 line will hold starting from a = 5.
Upvotes: 0
Reputation: 22979
You are skipping 2 altogether, because you start from a=1
and a=a+2
in the first iteration
Upvotes: 1