{"id":510,"date":"2012-08-02T12:50:52","date_gmt":"2012-08-02T16:50:52","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=510"},"modified":"2012-09-07T16:14:00","modified_gmt":"2012-09-07T20:14:00","slug":"project-euler-problem-28","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/02\/project-euler-problem-28\/","title":{"rendered":"Project Euler &#8211; Problem 28"},"content":{"rendered":"<p>Problem 28: Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:<\/p>\n<p><center><\/p>\n<pre>21 22 23 24 25\r\n20  7  8  9 10\r\n19  6  1  2 11\r\n18  5  4  3 12\r\n17 16 15 14 13<\/pre>\n<p><\/center><\/p>\n<p>It can be verified that the sum of the numbers on the diagonals is 101.<\/p>\n<p>What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang=\"java\">\r\nclass runner\r\n{\r\n\tpublic static void main (String[] args) throws java.lang.Exception\r\n\t{\r\n\t\tlong time = System.currentTimeMillis();\r\n\t\t\r\n\t\tint sum = 1; int diff_p = 0; int prev = 0;\r\n\t\tfor(int i=1;i<=1001;i+=2){\r\n\t\t\tint difference = 2*diff_p;\/\/from 8*diff_p\/4\r\n\t\t\tfor(int j=1;j<5;j++){\r\n\t\t\t\tsum += difference*j+prev;\r\n\t\t\t}\r\n\t\t\tprev = i*i;\r\n\t\t\tdiff_p++;\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(sum);\r\n\t\tSystem.out.println(\"time:\"+(System.currentTimeMillis()-time));\r\n\t}\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n<p>Reduces to..<br \/>\n<code><\/p>\n<pre lang='java'>\r\nclass runner\r\n{\r\n\tpublic static void main (String[] args) throws java.lang.Exception\r\n\t{\r\n\t\tlong time = System.currentTimeMillis();\r\n\t\t\r\n\t\tint sum = 1; int diff_p = 0; int prev = 0;\r\n\t\tfor(int i=1;i<=1001;i+=2){\r\n\t\t\tsum += diff_p+prev*4;\r\n\t\t\tprev = i*i;\r\n\t\t\tdiff_p+=20;\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(sum);\r\n\t\tSystem.out.println(\"time:\"+(System.currentTimeMillis()-time));\r\n\t}\r\n}<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 28: Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:<\/p>\n<p><center><\/p>\n<pre>21 22 23 24 25\r\n20  7  8  9 10\r\n19  6  1  2 11\r\n18  5  4  3 12\r\n17 16 15 14 13<\/pre>\n<p><\/center><\/p>\n<p>It can be verified that the sum of the numbers on the diagonals is 101.<\/p>\n<p>What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/510"}],"collection":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}