{"id":658,"date":"2012-08-18T00:41:14","date_gmt":"2012-08-18T04:41:14","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=658"},"modified":"2012-09-07T16:01:24","modified_gmt":"2012-09-07T20:01:24","slug":"project-euler-problem-62","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/18\/project-euler-problem-62\/","title":{"rendered":"Project Euler &#8211; Problem 62"},"content":{"rendered":"<p>Problem 62:<br \/>\nThe cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). <\/p>\n<p>In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.<\/p>\n<p>Find the smallest cube for which exactly five permutations of its digits are cube.<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang='java'>\r\npackage runner;\r\n\r\nimport java.math.BigInteger;\r\nimport java.util.HashSet;\r\nimport java.util.List;\r\nimport java.util.Vector;\r\n\r\nclass runner\r\n{\r\n\tprivate static void setup(HashSet<List<BigInteger>> store, int max, int limit){\r\n\t\tint len = 1;\r\n\t\tList<BigInteger> storeDigits = new Vector<BigInteger>(20);\r\n\t\tfor(int i=1;i<limit;i++){\r\n\t\t\tBigInteger current = new BigInteger(\"\"+i); current = current.pow(3);\r\n\t\t\tif(len < current.toString().length()){\r\n\t\t\t\tif(storeDigits.size() >= max) store.add(storeDigits);\r\n\t\t\t\tlen = current.toString().length();\r\n\t\t\t\tstoreDigits = new Vector<BigInteger>(10);\r\n\t\t\t}\r\n\t\t\tstoreDigits.add(current);\r\n\t\t}\r\n\t\tif(storeDigits.size() >= max) store.add(storeDigits);\r\n\t}\r\n\tpublic static boolean isPermutationOf(String strI, String strJ){\r\n\t\tint[] count = new int[48+10];\r\n\t\tfor(int i=0;i<strI.length();i++){\r\n\t\t\tcount[strI.charAt(i)]++;\r\n\t\t}\r\n\t\tfor(int i=0;i<strJ.length();i++){\r\n\t\t\tcount[strJ.charAt(i)]--;\r\n\t\t\tif(count[strJ.charAt(i)] < 0) return false;\r\n\t\t}\r\n\t\tfor(int i=48; i<48+10; i++){\r\n\t\t\tif(count[i] != 0) return false;\r\n\t\t}\r\n\t\treturn true;\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\r\n\t\tHashSet<List<BigInteger>> store = new HashSet<List<BigInteger>>(10);\r\n\t\tsetup(store, 5, 10000);\r\n\t\t\r\n\t\touterLoop:\r\n\t\tfor(List<BigInteger> set : store){\r\n\t\t\tboolean[] tried = new boolean[set.size()];\r\n\t\t\tfor(int i=0;i<set.size()-5;i++){\r\n\t\t\t\tString str_i = set.get(i).toString();\r\n\t\t\t\t\r\n\t\t\t\ttried[i] = true;\r\n\t\t\t\tint c = 1;\r\n\t\t\t\tfor(int j=i+1; j<set.size();j++){\r\n\t\t\t\t\tif(tried[j]) continue;\r\n\t\t\t\t\t\r\n\t\t\t\t\tif(isPermutationOf(str_i, set.get(j).toString())){\r\n\t\t\t\t\t\ttried[j] = true;\r\n\t\t\t\t\t\tc++;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\tif(c == 5){\r\n\t\t\t\t\tSystem.out.println(str_i);\r\n\t\t\t\t\tbreak outerLoop;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 62:<br \/>\nThe cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3). <\/p>\n<p>In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.<\/p>\n<p>Find the smallest cube for which exactly five permutations of its digits are cube.<\/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\/658"}],"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=658"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}