I am tying to make a python program that will generate the sum of primes for a number, but the program is not giving the correct result,please tell me why.
b=1
#generates a list of numbers.
while b<100:
b=b+1
x = 0.0
a = 0
d = 0
#generates a list of numbers less than b.
while x<b:
x=x+1
#this will check for divisors.
if (b/x)-int(b/x) == 0.0:
a=a+1
if a==2:
#if it finds a prime it will add it.
d=d+b
print d
I made it generate a list of primes successfully, but i could not get the primes to add.
This is the code that i used to generate a list of primes.
b=1
while b<1000:
b=b+1
n = b
x = 0.0
a = 0
while x<n:
x=x+1
if (n/x)-int(n/x) == 0.0:
a=a+1
if a==2:
print b
Kevin properly answered the question you asked. Permit me to answer the question you didn't ask, but should have: What is the best way to compute the sum of the primes less than n. The answer is to use a sieve:
This code implements the Sieve of Eratosthenes, summing primes as it goes. It works by repeatedly choosing the smallest uncrossed number (p in the code above, which is selected when
sieve[p]
isTrue
), then crossing out all of its multiples (i in the code above, which is incremented by p to compute multiples of p) starting from its square (since all smaller composites have already been crossed out). A sample use of the function isprint sumPrimes(100)
, which prints 1060, which is the correct answer.Note that Roland's answer does not implement the Sieve of Eratosthenes, even though he claims that it does; use of the modulo function is the giveaway that Roland's answer uses trial division, not the Sieve of Eratosthenes.
If you're interested in programming with prime numbers, I modestly recommend this essay at my blog.
if you're doing this for the sake of learning python there are more concise (>> less error-prone) ways of doing this. From your question I'm assuming you're trying to sum all the prime numbers below and including 100:
prints
1060
Just sum the list using the code you used to generate the list:
Your
d
variable is being reset during each iteration of your outer loop. Move the initialization out of that loop.Additionally, the
a == 2
check should only occur once per iteration of the outer loop. Move it out of the inner loop.Result:
While we're at it, let's try cleaning up the code so it's more comprehensible. You can move the inner loop into its own function, so readers can more clearly understand its purpose:
It's also useful to use variable names that describe what they represent:
for
loops are more idomatic thanwhile
loops that increment a counter:If you're feeling bold, you can use list comprehensions to cut down on the number of loops you use:
List comprehensions are a powerful tool in Python. Think of them as a for-loop in steroids. :-) You can use them to implement trial division, which is a simple way of finding primes.
It works like this:
The
prime_list
function:Now for the explanation. With the exception of 2, all prime numbers are odd. So all the odd numbers from 3 to
num
(100 in this case) are candidates for prime numbers. Let's generate a list of those as done at (a):For an odd number
c
to be prime, one must ensure thatc
modulo all previous odd numbersp
must be non-zero. Let's sayc
is 25.Then
p
is in:Now check
c
modulop
:As we know 25 % 5 == 0, so the second item in the list is
False
. However, for a number to be prime, all items in the list must be true:So 25 is not a prime.
Let's try again for
c
is 41:And indeed 41 is a prime.
So prime_list returns a list of primes:
To sum that up we simply use the
sum()
function:Edit: Based on the comments, I tried the improvement that WillNess suggested and a real sieve using sets:
Some timings for
num=100
:And
num=1000
:num = 5000
:And finally
num=50000
: