{"id":112,"date":"2013-01-18T12:26:45","date_gmt":"2013-01-18T20:26:45","guid":{"rendered":"http:\/\/willclausen.com\/?p=112"},"modified":"2014-04-07T16:11:30","modified_gmt":"2014-04-07T23:11:30","slug":"solution-project-euler-problem-8","status":"publish","type":"post","link":"https:\/\/willclausen.com\/?p=112","title":{"rendered":"Solution: Project Euler Problem 8"},"content":{"rendered":"<p>The Problem:<\/p>\n<p style=\"padding-left: 60px;\">Find the greatest product of five consecutive digits in the 1000-digit number.<\/p>\n<p style=\"padding-left: 60px;\">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>My Solution (in Java):<\/p>\n<pre class=\"brush: java; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n\/\/ Author: William Clausen\r\n\/\/\r\n\/\/ Date: Jan 7, 2013\r\n\/\/\r\n\/\/ This program solves Problem 8 from Project Euler.\r\n\r\npublic class Problem8 {\r\n\r\n\t\/\/ Datamembers to hold the number of numbers to be multiplied\r\n\t\/\/ and the number itself.\r\n\tpublic int numMult;\r\n\tpublic String nums;\r\n\r\n\tProblem8(int mult, String numbers)\r\n\t{\r\n\t\tnumMult = mult;\r\n\t\tnums = numbers;\r\n\t}\r\n\r\n\t\/\/ This method solves the problem.\r\n\tpublic int solve()\r\n\t{\r\n\t\t\/\/ Integers to store the largest product seen so far and\r\n\t\t\/\/ the most recent product.\r\n\t\tint bigProd = 1;\r\n\t\tint newProd = 1;\r\n\r\n\t\t\/\/ An array to keep track of which numbers are being multiplied together.\r\n\t\tint&#x5B;] multiples = new int&#x5B;numMult];\r\n\r\n\t\t\/\/ Now, loop through the whole number (which is a string) and store the last five\r\n\t\t\/\/ numbers seen in an array. Calculate the product of those five numbers\r\n\t\t\/\/ and check if it's the biggest product seen so far. If the new product is the\r\n\t\t\/\/ largest seen so far, store it.\r\n\t\tfor (int i = 0; i &lt; nums.length(); i++) {\r\n\t\t\tif (multiples&#x5B;i % numMult] != 0) {\r\n\t\t\t\tnewProd \/= multiples&#x5B;i % numMult];\r\n\t\t\t}\r\n\t\t\t\/\/ Here is some safety work to make sure only numbers are being multiplied.\r\n\t\t\tif (Character.isDigit(nums.charAt(i))) {\r\n\t\t\t\tmultiples&#x5B;i % numMult] = Integer.parseInt(nums.substring(i, i+1));\r\n\t\t\t} else {\r\n\t\t\t\t\/\/ If a non-number is encountered, then I decided the product should be\r\n\t\t\t\t\/\/ 0 for as long as that character would have been in the array of\r\n\t\t\t\t\/\/ stored numbers.\r\n\t\t\t\tfor (int j = 0; j &lt; numMult; j++) {\r\n\t\t\t\t\tmultiples&#x5B;j] = 0;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tnewProd = calcProd(multiples);\r\n\t\t\tif (newProd &gt; bigProd) {\r\n\t\t\t\tbigProd = newProd;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn bigProd;\r\n\t}\r\n\r\n\t\/\/ Method that calculates the product of numbers stored in an array.\r\n\tpublic int calcProd(int&#x5B;] nums)\r\n\t{\r\n\t\tint prod = 1;\r\n\r\n\t\tfor (int i = 0; i &lt; nums.length; i++) {\r\n\t\t\tif (nums&#x5B;i] == 0) {\r\n\t\t\t\treturn 0;\r\n\t\t\t} else {\r\n\t\t\t\tprod *= nums&#x5B;i];\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn prod;\r\n\t}\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Problem: Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450 My Solution (in Java): \/\/ Author: William Clausen \/\/ \/\/ Date: Jan 7, 2013 \/\/ \/\/ This program solves Problem 8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[9,27,44],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-project-euler-solutions","tag-january2013","tag-project-euler-problem-8","tag-project-euler-solutions"],"_links":{"self":[{"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":2,"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}