{"id":572,"date":"2012-08-06T23:21:47","date_gmt":"2012-08-07T03:21:47","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=572"},"modified":"2012-09-07T16:09:55","modified_gmt":"2012-09-07T20:09:55","slug":"project-euler-problem-43","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/06\/project-euler-problem-43\/","title":{"rendered":"Project Euler &#8211; Problem 43"},"content":{"rendered":"<p>Problem 43: The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.<\/p>\n<p>Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:<br \/>\n<!--more--><br \/>\n<center><\/p>\n<pre>d2d3d4=406 is divisible by 2\r\nd3d4d5=063 is divisible by 3\r\nd4d5d6=635 is divisible by 5\r\nd5d6d7=357 is divisible by 7\r\nd6d7d8=572 is divisible by 11\r\nd7d8d9=728 is divisible by 13\r\nd8d9d10=289 is divisible by 17<\/pre>\n<p><\/center><br \/>\nFind the sum of all 0 to 9 pandigital numbers with this property.<br \/>\n<code><\/p>\n<pre lang='java'>\r\nimport java.math.BigInteger;\r\nimport java.util.Vector;\r\n\r\nclass runner\r\n{\r\n\tprivate static boolean isValid(String x){\r\n\t\tif(Integer.valueOf(x.substring(1,4)) % 2 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(2,5)) % 3 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(3,6)) % 5 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(4,7)) % 7 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(5,8)) % 11 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(6,9)) % 13 != 0)\r\n\t\t\treturn false;\r\n\t\tif(Integer.valueOf(x.substring(7,10)) % 17 != 0)\r\n\t\t\treturn false;\r\n\r\n\t\treturn true;\r\n\t}\r\n\r\n\t\/\/Concept from Steinhaus\u2013Johnson\u2013Trotter\r\n\tpublic static void permute(String obj, Vector<String> arr){\r\n\t\tif(arr.size() == 0){\r\n\t\t\tarr.add(obj); \r\n\t\t\treturn;\r\n\t\t}\r\n\t\tVector<String> result = new Vector<String>();\r\n\r\n\t\tfor(String str: arr){\r\n\t\t\tfor(int i=0;i<str.length();i++){\r\n\t\t\t\tString pre = (i==0) ? \"\" : str.substring(0,i);\r\n\t\t\t\tString x = pre+obj+str.substring(i);\r\n\t\t\t\tif(x.length() != 10 || isValid(x))\r\n\t\t\t\t\tresult.add(x);\r\n\t\t\t}\r\n\t\t\tString x = str.substring(0,str.length())+obj;\r\n\t\t\tif(x.length() != 10 || isValid(x))\r\n\t\t\t\tresult.add(x);\r\n\t\t}\r\n\r\n\t\tarr.clear();\r\n\t\tarr.addAll(result);\r\n\t}\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\r\n\t\tVector<String> vector = new Vector<String>();\r\n\t\tfor(int i=0;i<10;i++){\r\n\t\t\tpermute(String.valueOf(i),vector);\r\n\t\t}\r\n\r\n\t\tBigInteger sum = new BigInteger(\"0\");\r\n\t\tfor(String str : vector){\r\n\t\t\t\/\/System.out.println(str);\r\n\t\t\tsum = sum.add(new BigInteger(str));\r\n\t\t}\r\n\r\n\t\tSystem.out.println(\"S:\"+sum);\r\n\r\n\t\tSystem.out.println(\"time: \"+(System.currentTimeMillis() - time));\r\n\t}\r\n}\r\n<\/pre>\n<p><\/code><br \/>\nNote:<br \/>\nThe isValid call should probably be called outside than when building the permutations.<br \/>\nHowever due to issues with memory on my computer when generating array sizes of 10!, it seemed the quickest way of injecting the call.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 43: The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.<\/p>\n<p>Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:<\/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\/572"}],"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=572"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/572\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}