{"id":675,"date":"2012-08-20T23:58:57","date_gmt":"2012-08-21T03:58:57","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=675"},"modified":"2012-09-07T16:00:03","modified_gmt":"2012-09-07T20:00:03","slug":"project-euler-problem-68","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/20\/project-euler-problem-68\/","title":{"rendered":"Project Euler &#8211; Problem 68"},"content":{"rendered":"<p>Problem 68:<br \/>\nConsider the following &#8220;magic&#8221; 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.<br \/>\n<img loading=\"lazy\" src=\"http:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1.gif\" alt=\"\" title=\"p_068_1\" width=\"250\" height=\"252\" class=\"aligncenter size-full wp-image-677\" srcset=\"https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1.gif 250w, https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1-150x150.gif 150w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><br \/>\n<!--more--><br \/>\nWorking clockwise, and starting from the group of three with the numerically lowest external node (4,3,2 in this example), each solution can be described uniquely. For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3.<\/p>\n<p>It is possible to complete the ring with four different totals: 9, 10, 11, and 12. There are eight solutions in total.<\/p>\n<p><center><\/p>\n<pre>Total\tSolution Set\r\n9\t4,2,3; 5,3,1; 6,1,2\r\n9\t4,3,2; 6,2,1; 5,1,3\r\n10\t2,3,5; 4,5,1; 6,1,3\r\n10\t2,5,3; 6,3,1; 4,1,5\r\n11\t1,4,6; 3,6,2; 5,2,4\r\n11\t1,6,4; 5,4,2; 3,2,6\r\n12\t1,5,6; 2,6,4; 3,4,5\r\n12\t1,6,5; 3,5,4; 2,4,6<\/pre>\n<p><\/center><br \/>\nBy concatenating each group it is possible to form 9-digit strings; the maximum string for a 3-gon ring is 432621513.<\/p>\n<p>Using the numbers 1 to 10, and depending on arrangements, it is possible to form 16- and 17-digit strings. What is the maximum 16-digit string for a &#8220;magic&#8221; 5-gon ring?<\/p>\n<p><img loading=\"lazy\" src=\"http:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_2.gif\" alt=\"\" title=\"p_068_2\" width=\"320\" height=\"325\" class=\"aligncenter size-full wp-image-676\" srcset=\"https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_2.gif 320w, https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_2-295x300.gif 295w\" sizes=\"(max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><code><\/p>\n<pre lang='java'>\r\nclass runner\r\n{\t\r\n\tprivate static void swap(byte[] set, int l, int k){\r\n\t\tbyte tmp = set[l];\r\n\t\tset[l] = set[k];\r\n\t\tset[k] = tmp;\r\n\t}\r\n\t\r\n\tpublic static boolean permute(byte[] store){\r\n\t\tint k = -1;\r\n\t\tfor(int i=0;i<store.length-1;i++){\r\n\t\t\tif(store[i] < store[i+1] &#038;&#038; ( k == -1 || k < i )) k = i;\/\/(arr[k] < arr[i])\r\n\t\t}\r\n\t\tif(k == -1) return false;\r\n\t\tint l = k+1;\r\n\t\tfor(int i=k;i<store.length;i++){\r\n\t\t\tif(store[k] < store[i]){\r\n\t\t\t\tif(store[l] > store[i]) l=i;\r\n\t\t\t}\r\n\t\t}\r\n\t\tswap(store,l,k);\r\n\r\n\t\tint diff = store.length - (k+1);\r\n\t\tfor(int i=0; i<diff\/2;i++){\r\n\t\t\tswap(store,i+(k+1),store.length-i-1);\r\n\t\t}\r\n\r\n\t\treturn true;\r\n\t}\r\n\tprivate static boolean verify(byte[] store){\r\n\t\t\/\/0 1 2 3 4\r\n\t\t\/\/5 6 7 8 9\r\n\t\tint sum = store[4]+store[0]+store[9];\r\n\t\tfor(int i=0;i<store.length\/2-1;i++){\r\n\t\t\tif(sum != store[i]+store[i+1]+store[i+5])\r\n\t\t\t\treturn false;\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\tpublic static String summation(byte[] store){\r\n\t\tStringBuffer result = new StringBuffer(16);\r\n\t\tint half = store.length\/2;\r\n\t\tbyte small=(byte)(store.length+1), idx=0;\r\n\t\tfor(int i=half;i<store.length;i++){\r\n\t\t\tif(store[i] < small){\r\n\t\t\t\tsmall = store[i];\r\n\t\t\t\tidx = (byte)i;\r\n\t\t\t}\r\n\t\t}\r\n\t\tfor(byte i=0;i<half;i++){\r\n\t\t\tint c = (i+idx)%half;\r\n\t\t\tresult.append(store[c+half]);\r\n\t\t\tresult.append(store[c]);\r\n\t\t\tresult.append(store[(c+1)%half]);\r\n\t\t}\r\n\t\t\r\n\t\treturn result.toString();\r\n\t}\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\tbyte[] store = new byte[10];\r\n\t\tfor(byte i=1;i<=store.length;i++)\r\n\t\t\tstore[i-1]=i;\r\n\t\t\r\n\t\tString max = \"\";\r\n\t\tdo{\r\n\t\t\tif(verify(store)){\r\n\t\t\t\tString cur = summation(store);\r\n\t\t\t\t\/*if(cur.length() == 16)\r\n\t\t\t\t\tSystem.out.println(cur);*\/\r\n\t\t\t\tif(cur.length() == 16 &#038;&#038; cur.compareTo(max) > 0){\r\n\t\t\t\t\tmax = cur;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}while(permute(store));\r\n\t\tSystem.out.println(max);\r\n\t\t\r\n\t\tSystem.out.println(\"time: \"+(System.currentTimeMillis() - time));\r\n\t}\r\n}\r\n<\/pre>\n<p><\/code><br \/>\nNote: Notice that you can wrap the inner circle of the 5-gon into an array&#8230;<br \/>\nNote2: Above code can be optimized by 1: Removing duplicates, 2. Assigning outer array 6-10 at the start.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 68:<br \/>\nConsider the following &#8220;magic&#8221; 3-gon ring, filled with the numbers 1 to 6, and each line adding to nine.<br \/>\n<img loading=\"lazy\" src=\"http:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1.gif\" alt=\"\" title=\"p_068_1\" width=\"250\" height=\"252\" class=\"aligncenter size-full wp-image-677\" srcset=\"https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1.gif 250w, https:\/\/www.joshho.com\/blog\/wp-content\/uploads\/2012\/08\/p_068_1-150x150.gif 150w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/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\/675"}],"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=675"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}