My solutions to the problems found at Project Euler.

Jump to: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 20 | 21 | 22 | 25 | 3-2 |

Problem 10


# /usr/bin/python
# Problem: Sum the total of all primes below 2,000,000.
# Solution: I've done this before. Find all primes, add them. Hopefully my list isn't too big
#           for memory, otherwise I might actually have to get creative (god forbid!)
#
#  Note: I had a long debate with myself about this one. I decided to research quick prime-finding
#        methods and came across a couple of sieve implementations. I've looked over the code for
#        quite awhile and am pretty familiar with how it works, but I don't think I would have ever
#        really come up with this on my own. I'm not a mathematician, and although I may be taking the
#        easy way out, I don't have too many problems with using an existing algorithm. I've learned 
#        a couple of cool tricks from this code:
#
#        I learned about:
#          [] * n syntax
#          xrange
#          Extended slicing syntax.
#
#        All in all, a victory. I will be using this algorithm in future solutions.

def primes(n):
  """ Returns  a list of primes < n """
  sieve = [True] * n
  for i in xrange(3,int(n**0.5)+1,2):
      if sieve[i]:
          sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
  return [2] + [i for i in xrange(3,n,2) if sieve[i]]
  
  
if __name__ == "__main__":
  total = 0
  for prime in primes(2000000):
    total += prime
    
  print total

jb