{"id":527,"date":"2012-08-04T17:41:40","date_gmt":"2012-08-04T21:41:40","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=527"},"modified":"2012-09-07T16:14:40","modified_gmt":"2012-09-07T20:14:40","slug":"project-euler-problem-32","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/04\/project-euler-problem-32\/","title":{"rendered":"Project Euler &#8211; Problem 32"},"content":{"rendered":"<p>Problem 32: Find the sum of all numbers that can be written as pandigital products.<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang='java'>\r\nimport java.util.HashSet;\r\nimport java.util.LinkedList;\r\nimport java.util.List;\r\n\r\nclass runner\r\n{\r\n\tpublic static class UnsignedBigInteger{\r\n\t\tprivate List<Byte> arr = new LinkedList<Byte>();\r\n\r\n\t\tprivate void _setup(double num){\r\n\t\t\tByte remainder = 0;\r\n\t\t\twhile(num > 0){\r\n\t\t\t\tremainder = new Byte((byte) (num % 10));\r\n\t\t\t\tarr.add(remainder);\r\n\t\t\t\tnum = (int)(num\/10);\r\n\t\t\t}\r\n\t\t}\r\n\t\tprivate void _setup(UnsignedBigInteger num){\r\n\t\t\tfor(int i=0; i<num.length(); i++){\r\n\t\t\t\tarr.add(num.get(i));\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tpublic UnsignedBigInteger(){\r\n\t\t\t_setup(0);\r\n\t\t}\r\n\r\n\t\tpublic UnsignedBigInteger(UnsignedBigInteger x){\r\n\t\t\t_setup(x);\r\n\t\t}\r\n\r\n\t\tpublic UnsignedBigInteger(double num){\r\n\t\t\t_setup(num);\r\n\t\t}\r\n\r\n\t\tpublic void add(int x){\r\n\t\t\t_add(new UnsignedBigInteger(x));\r\n\t\t}\r\n\r\n\t\tpublic void add(UnsignedBigInteger x){\r\n\t\t\t_add(x);\r\n\t\t}\r\n\r\n\t\tpublic void multiply(int x){\r\n\t\t\t_multiply(new UnsignedBigInteger(x));\r\n\t\t}\r\n\t\t\r\n\t\tpublic void multiply(UnsignedBigInteger x){\r\n\t\t\t_multiply(x);\r\n\t\t}\r\n\r\n\t\tpublic void power(int x){\r\n\t\t\t_power(new UnsignedBigInteger(x));\r\n\t\t}\r\n\r\n\t\tpublic void subtract(UnsignedBigInteger x){\r\n\t\t\tbyte pull = 0; boolean neg = false;\r\n\r\n\t\t\tfor(int a = 0; a<x.length(); a++){\r\n\t\t\t\tif(arr.size() <= a){ neg = true; break; }\r\n\t\t\t\tint diff = arr.get(a) - x.get(a) - pull;\r\n\t\t\t\tif(diff < 0){ pull = 1; diff+=10;}\r\n\t\t\t\telse{ pull = 0; }\r\n\t\t\t\tarr.set(a, (byte) diff);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tfor(int i = x.length(); i<arr.size(); i++){\r\n\t\t\t\tint diff = arr.get(i) - pull;\r\n\t\t\t\tif(diff < 0){ pull = 1; diff+=10;}\r\n\t\t\t\telse{ pull = 0; }\r\n\t\t\t\tarr.set(i, (byte) diff);\r\n\t\t\t}\r\n\r\n\t\t\tif(pull > 0 || neg){\r\n\t\t\t\tarr = new LinkedList<Byte>(); arr.add((byte) 0);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\twhile(arr.size()>0 && arr.get(arr.size()-1) == 0){\r\n\t\t\t\tarr.remove(arr.size()-1);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate void _power(UnsignedBigInteger arr2){\r\n\t\t\tif(arr2.toString().equals(\"0\")){\r\n\t\t\t\tarr = new LinkedList<Byte>(); arr.add((byte) 1);\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\twhile(arr2.toString().length() > 1 || arr2.toString().charAt(0) - 48 != 1){\r\n\t\t\t\tarr2.subtract(new UnsignedBigInteger(1));\r\n\t\t\t\tUnsignedBigInteger original = this.clone();\r\n\t\t\t\tthis.add(original);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/*if x is bigger, return -1\r\n\t\t * \r\n\t\t *\/\r\n\t\tpublic int compareTo(UnsignedBigInteger x){\r\n\t\t\tbyte pull = 0; boolean neg = false;\r\n\t\t\tUnsignedBigInteger tmp = new UnsignedBigInteger(this);\r\n\r\n\t\t\tfor(int a = 0; a<x.length(); a++){\r\n\t\t\t\tif(tmp.length() <= a){ neg = true; break; }\r\n\t\t\t\tint diff = tmp.get(a) - x.get(a) - pull;\r\n\t\t\t\tif(diff < 0){ pull = 1; diff+=10;}\r\n\t\t\t\telse{ pull = 0; }\r\n\t\t\t\ttmp.set(a, (byte) diff);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tfor(int i = x.length(); i<tmp.length(); i++){\r\n\t\t\t\tint diff = tmp.get(i) - pull;\r\n\t\t\t\tif(diff < 0){ pull = 1; diff+=10;}\r\n\t\t\t\telse{ pull = 0; }\r\n\t\t\t\ttmp.set(i, (byte) diff);\r\n\t\t\t}\r\n\r\n\t\t\tif(pull > 0 || neg)\r\n\t\t\t\treturn -1;\r\n\t\t\tif(tmp.toString().length() == 1 || tmp.toString().charAt(0) - 48 == 0) return 0;\r\n\t\t\treturn 1;\r\n\t\t}\r\n\r\n\t\tprivate void _multiply(UnsignedBigInteger arr2){\r\n\t\t\tif(arr2.toString().equals(\"0\")){\r\n\t\t\t\tarr = new LinkedList<Byte>(); arr.add((byte) 0);\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tUnsignedBigInteger original = this.clone();\r\n\t\t\tUnsignedBigInteger counter = arr2.clone();\r\n\t\t\twhile(counter.toString().length() > 1 || counter.toString().charAt(0) - 48 != 1){\r\n\t\t\t\tcounter.subtract(new UnsignedBigInteger(1));\r\n\t\t\t\tthis.add(original);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate void _add(UnsignedBigInteger arr2){\r\n\t\t\tbyte carry = 0;\r\n\t\t\tfor(int n = 0; n<arr.size() &#038;&#038; n < arr2.length(); n++){\r\n\t\t\t\tint sum = carry + arr.get(n) + arr2.get(n);\r\n\r\n\t\t\t\tByte remainder = new Byte((byte) (sum % 10));\r\n\t\t\t\tarr.set(n, remainder);\r\n\t\t\t\tcarry = (byte) Math.floor(sum\/10);\r\n\t\t\t}\r\n\r\n\t\t\tif(arr.size() == arr2.length()){\r\n\t\t\t}else{\r\n\t\t\t\tUnsignedBigInteger smaller, bigger;\r\n\t\t\t\tif(arr.size() < arr2.length()){\r\n\t\t\t\t\tsmaller = this;\r\n\t\t\t\t\tbigger = arr2;\r\n\t\t\t\t}else{\r\n\t\t\t\t\tsmaller = arr2;\r\n\t\t\t\t\tbigger = this;\r\n\t\t\t\t}\r\n\r\n\r\n\t\t\t\tfor(int n = smaller.length(); n< bigger.length(); n++){\r\n\t\t\t\t\tint sum = carry + bigger.get(n);\r\n\r\n\t\t\t\t\tByte remainder = new Byte((byte) (sum % 10));\r\n\t\t\t\t\tthis.set(n, remainder);\r\n\t\t\t\t\tcarry = (byte) (sum\/10);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t\/\/Append rest of carry to front\r\n\t\t\tif(carry > 0){\r\n\t\t\t\twhile(carry > 0){\r\n\t\t\t\t\tByte remainder = new Byte((byte) (carry % 10));\r\n\t\t\t\t\tarr.add(remainder);\r\n\t\t\t\t\tcarry = (byte) (carry\/10);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tpublic byte get(int i){\r\n\t\t\treturn arr.get(i);\r\n\t\t}\r\n\t\tpublic void set(int index, byte n){\r\n\t\t\tn = (byte) (n % 10);\r\n\t\t\tif(arr.size() <= index){\r\n\t\t\t\tarr.add(n);\r\n\t\t\t}else{\r\n\t\t\t\tarr.set(index, n);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tpublic int length(){\r\n\t\t\treturn arr.size();\r\n\t\t}\r\n\t\tpublic UnsignedBigInteger clone(){\r\n\t\t\treturn new UnsignedBigInteger(this);\r\n\t\t}\r\n\t\tpublic String toString(){\r\n\t\t\tStringBuilder x = new StringBuilder();\r\n\t\t\tfor(int i = arr.size()-1; i>-1; i--){\r\n\t\t\t\tbyte y = arr.get(i);\r\n\t\t\t\tx.append(y);\r\n\t\t\t}\r\n\t\t\tif(x.length() == 0) x.append(0);\r\n\t\t\treturn x.toString();\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate static boolean removeFromSingular(HashSet<Integer> singular, UnsignedBigInteger arr){\r\n\t\tfor(int i = arr.length()-1; i>-1; i--){\r\n\t\t\tbyte y = arr.get(i);\r\n\t\t\tif(!singular.contains(y)) return false;\r\n\t\t\tsingular.remove(y);\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\t@SuppressWarnings(\"unchecked\")\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\tUnsignedBigInteger sum = new UnsignedBigInteger(0);\r\n\t\t\r\n\t\tHashSet<String> answers = new HashSet<String>();\r\n\t\tHashSet<Byte> singular_model = new HashSet<Byte>();\r\n\t\tfor(byte i=1;i<10;i++){singular_model.add(i);}\r\n\t\tUnsignedBigInteger max_limit = new UnsignedBigInteger(9999);\r\n\t\t\r\n\t\tfor(UnsignedBigInteger a= new UnsignedBigInteger(2); a.compareTo(max_limit) < 0; a.add(1)){\r\n\t\t\tHashSet<Integer> singular_a = (HashSet<Integer>) singular_model.clone();\r\n\t\t\tif(!removeFromSingular(singular_a, a)) continue;\r\n\t\t\t\r\n\t\t\tUnsignedBigInteger b_start = new UnsignedBigInteger(100), b_end = new UnsignedBigInteger(1000);\r\n\t\t\tif(a.length() == 1){\r\n\t\t\t\tb_start = new UnsignedBigInteger(1000);\r\n\t\t\t\tb_end = new UnsignedBigInteger(10000);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tfor(UnsignedBigInteger b= new UnsignedBigInteger(b_start); b.compareTo(b_end) < 0; b.add(1)){\t\t\t\r\n\t\t\t\tHashSet<Integer> singular_b = (HashSet<Integer>) singular_a.clone();\r\n\t\t\t\tif(!removeFromSingular(singular_b, b)) continue;\r\n\t\t\t\t\r\n\t\t\t\tUnsignedBigInteger prod = new UnsignedBigInteger(a);\r\n\t\t\t\tprod.multiply(b);\r\n\t\t\t\t\r\n\t\t\t\tif(a.length() + b.length() + prod.length() < 9) continue;\r\n\t\t\t\tif(a.length() + b.length() + prod.length() > 9) break;\r\n\t\t\t\tif(!removeFromSingular(singular_b, prod)) continue;\r\n\t\t\t\tif(answers.contains(prod.toString())) continue;\r\n\t\t\t\tanswers.add(prod.toString());\r\n\t\t\t\tsum.add(prod);\r\n\t\t\t\tSystem.out.println(a + \" x \"+ b + \" = \"+prod);\r\n\t\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><br \/>\nNote: Definitely not my best work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 32: Find the sum of all numbers that can be written as pandigital products.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-project-euler"],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"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=527"}],"version-history":[{"count":2,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":778,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/527\/revisions\/778"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}