{"id":103,"date":"2013-01-17T21:56:46","date_gmt":"2013-01-18T05:56:46","guid":{"rendered":"http:\/\/willclausen.com\/?p=103"},"modified":"2013-01-17T21:56:46","modified_gmt":"2013-01-18T05:56:46","slug":"solution-project-euler-problem-6","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=103","title":{"rendered":"Solution: Project Euler Problem 6"},"content":{"rendered":"<p>The Problem:<\/p>\n<p style=\"padding-left: 30px;\">Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.<\/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 6 form Project Euler.\r\n\/* Problem Statement:\r\n * Find the difference between the sum of the squares of the first one hundred \r\n * natural numbers and the square of the sum.\r\n *\/\r\n\r\n\r\nimport java.lang.Math; \/\/ For squaring numbers\r\n\r\npublic class Problem6 {\r\n\r\n\t\/\/ Data members to store the number of natural numbers to use,\r\n\t\/\/ the sum of the squares and the square of the sum.\r\n\tpublic int sumSquare;\r\n\tpublic int squareSum;\r\n\tpublic int Num;\r\n\t\r\n\tProblem6(int num)\r\n\t{\r\n\t\tNum = num;\r\n\t\tsumSquare = 0;\r\n\t\tsquareSum = 0;\r\n\t}\r\n\t\r\n\t\/\/ Method that solves the problem and returns the result.\r\n\t\/\/ The heavy lifting is done in the two helper functions,\r\n\t\/\/ sumSquares(int) and squaredSum(int).\r\n\tpublic int solve()\r\n\t{\r\n\t\tsumSquare = sumSquares(Num);\r\n\t\tsquareSum = squaredSum(Num);\r\n\t\t\r\n\t\treturn squareSum - sumSquare;\r\n\t}\r\n\t\r\n\t\/\/ This function computes the sum of the squares of the natural numbers\r\n\t\/\/ from 1 to num.\r\n\tprivate int sumSquares(int num)\r\n\t{\r\n\t\tint sum = 0;\r\n\t\t\r\n\t\tfor (int i = 1; i &lt;= num; i++) {\r\n\t\t\tsum += (int) Math.pow(i,2);\r\n\t\t}\r\n\t\t\r\n\t\treturn sum;\r\n\t}\r\n\t\r\n\t\/\/ This function computes and returns the square of the sum of the natural \r\n\t\/\/ numbers from 1 to num.\r\n\tprivate int squaredSum(int num)\r\n\t{\r\n\t\tint totalSum = (num)*(num+1);\r\n\t\ttotalSum = totalSum\/2;\r\n\t\ttotalSum = (int) Math.pow(totalSum, 2);\r\n\t\t\r\n\t\treturn totalSum;\r\n\t}\r\n}\r\n\r\n\/\/ Solution: 25164150\r\n\r\n<\/pre>\n<p>Straightforward problem. As always, I love feedback on how this solution could be improved!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Problem: Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. My solution (in Java): \/\/ Author: William Clausen \/\/ \/\/ Date: Jan 7, 2013 \/\/ \/\/ This program solves Problem 6 form Project Euler. \/* Problem Statement: * Find the difference [&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,25,19],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-project-euler-solutions","tag-january2013","tag-project-euler-problem-6","tag-project-euler-solution"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=103"}],"version-history":[{"count":1,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":104,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/103\/revisions\/104"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}