Sep-14-2018, 05:49 PM
No, you should change
But you will have other problems. After the test for each prime, you are adding the prime. Say you get to 9. You will check 9 against 2, and find it doesn't divide evenly. Then you will save 9 as a prime and add it to sum, but 9 is not prime.
You need to check that all of the primes do not divide evenly in the number you are checking. I would typically do it this way:
for i in range(1, int(test**0.5)+1, int(primes)):to
for prime in primes:You can loop directly through the items in a list this way. So the first time through the loop it will be 2, the second time it will be 3, and the 5, and so on.
But you will have other problems. After the test for each prime, you are adding the prime. Say you get to 9. You will check 9 against 2, and find it doesn't divide evenly. Then you will save 9 as a prime and add it to sum, but 9 is not prime.
You need to check that all of the primes do not divide evenly in the number you are checking. I would typically do it this way:
for counter in range(3, 10000, 2): for prime in primes: if counter % prime == 0: break else: primes.append(counter) total = sum(primes)The first loop goes through the odd numbers (using step = 2) from 3 to 10000. The second loop goes through any primes you've found. The key is the else statement after the inner for loop. Else statements after loops trigger if the loop exits without a break statement. In this case, the break statement happens if a prime factor is found. So the else statement only triggers if no prime factors are found. Then at the end, you use the sum function to total the primes, rather than keeping a running total.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures