Project Euler - Problem 10


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. print "\nProject Euler - Problem 10"
  4. print "Find the sum of all primes below two million."
  5.  
  6. # Primes are divisible only by themselves
  7. n = 2000000
  8. nums = range(0,n)
  9. sqrt = int(n ** 0.5)
  10. a = []
  11. a.append(1)
  12. for x in range(1,n+1):
  13. a.append(1)
  14.  
  15. for m in range(2,sqrt):
  16. if a[m] == 1:
  17. k = m*m
  18. while k <= n:
  19. a[k] = 0
  20. k += m
  21.  
  22. x= sum([i*j for i,j in zip(a,nums)])
  23.  
  24. print x

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.