-7
##Problem 10
primes=[]
sumofprimes=0
for number in range(1,2000000):
##This goes through all the potential primes.
    p=0
    for tester in range(2,number):
        if ((number%tester)==0):
            break
        else:
##every time the number is not divisible by something it adds 1 to p and when everything up to the number is not divisible by anything then it is a prime.
            p=p+1
        if(p==(number-2)):

the "number minus 2" is because lets say you have the prime "3" it will only test "2" so p will equal 1 or (3-2).

            sumofprimes=sumofprimes+number
print(sumofprimes)

1 Answers1

0

You could upgrade your second loop using this theorem : Fermat's little theorem

Basically, if sqrt(p) is prime, p is. So you can create a loop like this (not tested):

for tester in range(3,sqrt(number), 2):
    if ((number%tester)==0):
        break

(no need to past through multiple of 2)

LudwigVonKoopa
  • 158
  • 2
  • 13