Wednesday, November 7, 2012

Euler Problem 10 - sum of primes below 2m

3


In the unlikely event that anyone is reading this blog, I am in my late forties with no programming experience since I bought a ZX81, so these are not the best programs in the world but I am learning!



Program approach

  • myPrimes is the list of primes starting at 2 as the first.
  • myMaximum is the limit of the prime numbers I want to find
  • testedNumber is 3 - i.e. the first number after 2 as the first seed prime
  • while loop carries on while the last prime found in myPrimes is less than myMaximum


Start the while loop assuming that testedNumber is a prime (i.e. isAPrime = True) then iterate through all the primes found to date in myPrimes to see if one of them goes into testedNumber.  If one does then you can break the while loop.

After you have iterated through the primes in myPrimes If isAPrime remains true at the end then the testedNumber gets added to the list of primes and the number of primes found goes up by one.

What I learned

How to use while loops.  In particular you can use another number as a counter in the while loop to keep track of stuff while the while loop is still working - in my case this incrementing primesFound by 1 and then using it to index myPrimes.  In other words don't confuse for loops with while loops - while loops can monitor whether a condition is met or not without having to have an index.

Using the break function in for loops to stop wasting time/processing power.

Watch out for going through the while loop one too many times.  In my case the while loop was looking at the last primeFound and stopped when it was over 2m.  But this means that the last prime over 2m was included in the list.


'''
Created on 2012/09/24

@author: malcolm
'''
myPrimes=[2]
primesFound = 1
print(primesFound, myPrimes[primesFound-1])
myMaximum = 2000000
# primesWanted = 10001 carry over from problem 7
testedNumber = 3
while myPrimes[primesFound-1] < myMaximum :
    isAPrime = True #testedNumber assumed to be a prime until flagged as False
    for prime in myPrimes :
        if testedNumber % prime == 0:
            isAPrime = False
            break #no point in carrying on with the for loop as testedNumber proven not to be a prime
    if isAPrime: #isAPrime remains True after the for loop above is completed
        
        myPrimes.append(testedNumber)
        primesFound += 1 #only treat a prime as found if you add it to myPrimes!  this keeps the while loop OK
        if primesFound % 2000 == 0: print(primesFound,testedNumber) #help keep track of progress
    testedNumber += 1  #the while loop will come to this last statement, test the expression and then carry on if needed
        
print(myPrimes)

cumulativePrimeTotal = 0
for prime in myPrimes:
    cumulativePrimeTotal += prime
    
cumulativePrimeTotal -= myPrimes[len(myPrimes)-1] #take off very last prime as you went through the loop once to many times

print("the total is", cumulativePrimeTotal)

No comments:

Post a Comment