{"id":143,"date":"2009-07-16T21:09:29","date_gmt":"2009-07-17T05:09:29","guid":{"rendered":"http:\/\/www.madpickles.org\/rokjoo\/?p=143"},"modified":"2009-07-16T21:09:29","modified_gmt":"2009-07-17T05:09:29","slug":"project-euler-problems-3-and-5","status":"publish","type":"post","link":"https:\/\/www.madpickles.org\/rokjoo\/2009\/07\/16\/project-euler-problems-3-and-5\/","title":{"rendered":"Project Euler Problems #3 and #5"},"content":{"rendered":"<p>I got ahead of myself last post and skipped problem 3. Maybe it&#8217;s because it wasn&#8217;t that interesting: what&#8217;s the largest prime factor of 600851475143?<\/p>\n<p>Being an odd number meant 2 was not the largest prime factor, and no even numbers are prime so this simple trial division function seemed to work pretty well:<\/p>\n<pre>\r\ndef largestPrimeFactor(num):\r\n factor = 3\r\n while factor <= num:\r\n  if num % factor == 0:\r\n   num \/= factor\r\n  else:\r\n   factor += 2\r\n return factor\r\n\r\niterations = 100\r\nt = timeit.Timer(\"largestPrimeFactor(600851475143)\",\r\n                 \"from __main__ import largestPrimeFactor\")\r\n\r\nprint \"Answer: %d. %d iterations took %.3f seconds\" % \\\r\n       (largestPrimeFactor(600851475143), iterations, \\\r\n        t.repeat(1,iterations)[0])\r\n<\/pre>\n<p>Answer: 6857. 100 iterations took 0.949 seconds<\/p>\n<p>It relies on the fact that every composite number can be represented by a unique set of prime factors. For example 8 is 2 * 2 * 2, 12 is 2 * 2 * 3, 42 is 2 * 3 * 7. This bit of knowledge proved very useful in problem #5, which asked for the first number evenly divisible by the numbers 1 - 20. For the first time, I was able to solve a problem with just pen and paper.<\/p>\n<p>I started by listing the prime numbers less than 20 as each of these needed to be represented in the product: 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19. Then I filled in the blanks with the unique prime factorizations of the other numbers:<\/p>\n<p>4 is 2 * 2, so now the product is: 2 * 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19<br \/>\n6 is 2 * 3 which is already accounted for<br \/>\n8 is 2 * 2 * 2, so now the product is: 2 * 2 * 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19<br \/>\n9 is 3 * 3, so I added another 3: 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13 * 17 * 19<br \/>\n10 is 2 * 5 which is already accounted for<br \/>\n12 is 2 * 2 * 3 which is already accounted for<br \/>\n14 is 2 * 7, already accounted for<br \/>\n15 is 3 * 5, already accounted for<br \/>\n16 is 2 * 2 * 2 * 2, so I add another 2: 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13 * 17 * 19<br \/>\n18 is 2 * 3 * 3, already accounted for<br \/>\n20 is 2 * 2 * 5, already accounted for<\/p>\n<p>I did write a quick program to verify I was correct:<\/p>\n<pre>\r\nx = 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13 * 17 * 19\r\nfor d in xrange(1,21):\r\n print '%d \\ %d = %d' % (x,d,(x % d))\r\n<\/pre>\n<p>which outputs:<\/p>\n<p>232792560 \\ 1 = 0<br \/>\n232792560 \\ 2 = 0<br \/>\n232792560 \\ 3 = 0<br \/>\n232792560 \\ 4 = 0<br \/>\n232792560 \\ 5 = 0<br \/>\n232792560 \\ 6 = 0<br \/>\n232792560 \\ 7 = 0<br \/>\n232792560 \\ 8 = 0<br \/>\n232792560 \\ 9 = 0<br \/>\n232792560 \\ 10 = 0<br \/>\n232792560 \\ 11 = 0<br \/>\n232792560 \\ 12 = 0<br \/>\n232792560 \\ 13 = 0<br \/>\n232792560 \\ 14 = 0<br \/>\n232792560 \\ 15 = 0<br \/>\n232792560 \\ 16 = 0<br \/>\n232792560 \\ 17 = 0<br \/>\n232792560 \\ 18 = 0<br \/>\n232792560 \\ 19 = 0<br \/>\n232792560 \\ 20 = 0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got ahead of myself last post and skipped problem 3. Maybe it&#8217;s because it wasn&#8217;t that interesting: what&#8217;s the largest prime factor of 600851475143? Being an odd number meant 2 was not the largest prime factor, and no even numbers are prime so this simple trial division function seemed to work pretty well: def [&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\/143"}],"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=143"}],"version-history":[{"count":4,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/posts\/143\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madpickles.org\/rokjoo\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}