{"id":169,"date":"2009-10-07T20:52:41","date_gmt":"2009-10-08T04:52:41","guid":{"rendered":"http:\/\/www.madpickles.org\/rokjoo\/?p=169"},"modified":"2010-02-18T13:26:31","modified_gmt":"2010-02-18T21:26:31","slug":"project-euler-problems-6-7-8","status":"publish","type":"post","link":"https:\/\/www.madpickles.org\/rokjoo\/2009\/10\/07\/project-euler-problems-6-7-8\/","title":{"rendered":"Project Euler Problems #6, #7, #8"},"content":{"rendered":"<p>I took a break from <a href=\"http:\/\/www.projecteuler.net\">projecteuler<\/a> to whip up <a href=\"http:\/\/rokjoo.madpickles.org\/2009\/07\/31\/first-webos-app\/\">a webOS application<\/a> before my <a href=\"http:\/\/rokjoo.madpickles.org\/2009\/09\/02\/tahoe-rim-trail-thru-hike\/\">Tahoe Rim Trail Thru-hike<\/a>. Upon returning I started skiming some technical books and started playing around with the <a href=\"http:\/\/http:\/\/www.facebook.com\/careers\/puzzles.php\">Facebook Puzzles<\/a>. In general, starting lots of threads is exciting and gives me the feeling that I am learning a lot; however, I believe it&#8217;s also important to finish (most) of what one starts. I never got my webOS application to a state where I felt comfortable placing it in the app store, but I did get it to the point where it was usable during my hike. Perhaps I should have continued working on it as there are at least two apps in the store which provide similar functionality. I finished the TRT, and now it&#8217;s time to start making more progress with Project Euler.<\/p>\n<p>Problem #6<br \/>\nFind the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. This didn&#8217;t seem like a very interesting problem. I did learn about python&#8217;s ** operator which can raise any value to the power of another value. This made it easy to answer the question with a 1 line function:<\/p>\n<pre>\r\ndef p6(r):\r\n return sum(r)** 2 - sum([x** 2 for x in r])\r\n\r\nprint p6(range(101))\r\n<\/pre>\n<p>After reading the forums, as one might expect, there are mathematical formulas for these summations which don&#8217;t require iterating through the entire list&#8230;<\/p>\n<p>Problem #7<br \/>\nFind the 10001<sup>st<\/sup> prime number. I sure any mathematician would balk at this, but my gut feeling is that there&#8217;s a prime on average every 10 numbers. So I whipped up a quick version of the Sieve of Eratosthenes and set the limit to 100000. In less than a second on my little Eee PC I had printed the first 9592 primes. It turned out I needed to raise the limit a bit, closer to 105000<\/p>\n<pre>\r\nimport sys\r\nmax = int(sys.argv[1])\r\nmaxM = max - 2\r\nr = range(2,max)\r\nc = 1\r\nfor n in r:\r\n m = n - 2\r\n if r[m] != 0:\r\n  print \"prime %d is %d\" % (c,n)\r\n  c += 1\r\n  m += n\r\n  while m < maxM:\r\n   r[m] = 0\r\n   m += n\r\n<\/pre>\n<p>Then run it as python problem0007.py 105000. Just for kicks I tried setting the limit to 1000000 and in under 10 seconds found the first 78498 primes (the last one being 999983).<\/p>\n<p>Problem #8<br \/>\nFind the greatest product of five consecutive digits in the 1000-digit number<\/p>\n<p>73167176531330624919225119674426574742355349194934<br \/>\n96983520312774506326239578318016984801869478851843<br \/>\n85861560789112949495459501737958331952853208805511<br \/>\n12540698747158523863050715693290963295227443043557<br \/>\n66896648950445244523161731856403098711121722383113<br \/>\n62229893423380308135336276614282806444486645238749<br \/>\n30358907296290491560440772390713810515859307960866<br \/>\n70172427121883998797908792274921901699720888093776<br \/>\n65727333001053367881220235421809751254540594752243<br \/>\n52584907711670556013604839586446706324415722155397<br \/>\n53697817977846174064955149290862569321978468622482<br \/>\n83972241375657056057490261407972968652414535100474<br \/>\n82166370484403199890008895243450658541227588666881<br \/>\n16427171479924442928230863465674813919123162824586<br \/>\n17866458359124566529476545682848912883142607690042<br \/>\n24219022671055626321111109370544217506941658960408<br \/>\n07198403850962455444362981230987879927244284909188<br \/>\n84580156166097919133875499200524063689912560717606<br \/>\n05886116467109405077541002256983155200055935729725<br \/>\n71636269561882670428252483600823257530420752963450<\/p>\n<p>The first thing I did was copy the number into a file and write some code that would concatonate the numbers into a single string with no line breaks:<\/p>\n<pre>\r\nline = \"\"\r\nfile = open(\"problem0008.txt\",\"r\")\r\nwhile True:\r\n temp = file.readline()\r\n if not temp:\r\n  break\r\n line += temp[:-1]\r\nfile.close()\r\n<\/pre>\n<p>Then even though there's a bunch of unecessary multiplication, I wrote this function that can find the largest product of any n consecutive numbers:<\/p>\n<pre>\r\ndef findLargestProduct(span,line):\r\n no0 = line.split('0')\r\n m = 0\r\n for x in filter((lambda x: len(x) >= span),no0):\r\n  nums = list(x)\r\n  while len(nums) >= span:\r\n   m = max(m,reduce((lambda x, y: int(x) * int(y)),nums[:span]))\r\n   del nums[0]\r\n return m\r\n<\/pre>\n<p>I put in one optimization to only work with sub-strings equal to or longer the requested span, in this case 5, that do not contain a zero.<\/p>\n<p>Then this finds the answer: print findLargestProduct(5,line)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I took a break from projecteuler to whip up a webOS application before my Tahoe Rim Trail Thru-hike. Upon returning I started skiming some technical books and started playing around with the Facebook Puzzles. In general, starting lots of threads is exciting and gives me the feeling that I am learning a lot; however, I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[18,11,19,17],"_links":{"self":[{"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":4,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts\/169\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}