{"id":759,"date":"2012-02-17T17:20:48","date_gmt":"2012-02-17T17:20:48","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=759"},"modified":"2012-02-17T16:08:13","modified_gmt":"2012-02-17T16:08:13","slug":"project-euler-problem-10-sum-for-primes-to-2-million","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2012\/02\/project-euler-problem-10-sum-for-primes-to-2-million\/","title":{"rendered":"Project Euler Problem 10 &#8211; Sum for primes to 2 million"},"content":{"rendered":"<p>At first I attempted to solve this using the sieve code I wrote for <a href=\"http:\/\/41j.com\/blog\/2012\/02\/euler-problem-3-python-solution\/\">problem 3<\/a>. However that code used Python lists and was removing elements as it progressed. That was horribly inefficient. So I&#8217;ve learnt to use Python arrays, which I was surprised to learn are not a native data structure in Python. Anyway, for my reference here is the code:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport math\r\n\r\nfrom array import array\r\n\r\ndef getprimes(input):\r\n  myarray = array('L',range(input))\r\n  myarray&#x5B;1] = 0\r\n\r\n  for n in myarray:\r\n    if n != 0:\r\n      for j in xrange(n+n,int(input),n):\r\n        myarray&#x5B;j] = 0\r\n\r\n  retvals = &#x5B;]\r\n\r\n  for n in myarray:\r\n    if n != 0:\r\n      retvals.append(n)\r\n\r\n  return retvals\r\n\r\np = getprimes(2000000)\r\n\r\nsum = 0\r\nfor i in p:\r\n  sum += i\r\n\r\nprint sum\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>At first I attempted to solve this using the sieve code I wrote for problem 3. However that code used Python lists and was removing elements as it progressed. That was horribly inefficient. So I&#8217;ve learnt to use Python arrays, which I was surprised to learn are not a native data structure in Python. Anyway, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-cf","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=759"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"predecessor-version":[{"id":761,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/759\/revisions\/761"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}