{"id":449,"date":"2012-07-29T23:57:58","date_gmt":"2012-07-30T03:57:58","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=449"},"modified":"2012-09-07T16:16:47","modified_gmt":"2012-09-07T20:16:47","slug":"project-euler-problem-16","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/07\/29\/project-euler-problem-16\/","title":{"rendered":"Project Euler &#8211; Problem 16"},"content":{"rendered":"<p>Problem 16: What is the sum of the digits of the number 2^1000?<br \/>\n<!--more--><\/p>\n<p>Tried my hand at writing my own UnsignedBigInt&#8230; (tailored for this question)<br \/>\n<code><\/p>\n<pre lang='java'>\r\nimport java.util.LinkedList;\r\nimport java.util.List;\r\n\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\t\t\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\tarr.add((byte)num);\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\t\t\r\n\t\tpublic UnsignedBigInteger(){\r\n\t\t\t_setup(0);\r\n\t\t}\r\n\t\t\r\n\t\tpublic UnsignedBigInteger(UnsignedBigInteger x){\r\n\t\t\t_setup(x);\r\n\t\t}\r\n\t\t\r\n\t\tpublic UnsignedBigInteger(double num){\r\n\t\t\t_setup(num);\r\n\t\t}\r\n\t\t\r\n\t\tpublic void add(int x){\r\n\t\t\t_add(new UnsignedBigInteger(x));\r\n\t\t}\r\n\t\t\r\n\t\tpublic void add(UnsignedBigInteger x){\r\n\t\t\t_add(x);\r\n\t\t}\r\n\t\t\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 power(int x){\r\n\t\t\t_power(new UnsignedBigInteger(x));\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ Problem only requires int subtraction\r\n\t\tpublic void subtract(int x){\r\n\t\t\tint cur = x;\r\n\t\t\tint div = x;\r\n\t\t\t\r\n\t\t\tbyte pull = 0;\r\n\t\t\tfor(int n = 0; n<arr.size() &#038;&#038; (div > 0 || pull > 0); n++){\r\n\t\t\t\tdiv = cur\/10;\r\n\t\t\t\tint rmd = (cur%10);\r\n\t\t\t\tint diff = arr.get(n) - rmd - pull;\r\n\t\t\t\tif(diff < 0){\r\n\t\t\t\t\tdiff += 10;\r\n\t\t\t\t\tpull = 1;\r\n\t\t\t\t}else{\r\n\t\t\t\t\tpull = 0;\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\tarr.set(n, (byte)diff);\r\n\t\t\t\tcur = div;\r\n\t\t\t}\r\n\t\t\tif (pull != 0){\r\n\t\t\t\tarr = new LinkedList<Byte>(); arr.add((byte) 0);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\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\t\t\t\r\n\t\t\twhile(arr2.toString().length() > 1 || arr2.toString().charAt(0) - 48 != 1){\r\n\t\t\t\tarr2.subtract(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\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\t\t\t\r\n\t\t\tUnsignedBigInteger original = this.clone();\r\n\t\t\twhile(arr2.toString().length() > 1 || arr2.toString().charAt(0) - 48 != 1){\r\n\t\t\t\tarr2.subtract(1);\r\n\t\t\t\tthis.add(original);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\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\t\t\t\t\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\t\t\t\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\t\t\t\t\t\r\n\t\t\t\t\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\t\t\t\t\t\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\t\t\t\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\t\t\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\t\t\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\tboolean hit = false;\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\tif(!hit && y==0 )continue;\r\n\t\t\t\telse hit = true;\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\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\tUnsignedBigInteger num = new UnsignedBigInteger(2);\r\n\t\tnum.power(1000);\r\n\r\n\t\tdouble sum = 0;\r\n\t\tfor(int i = 0; i< num.length(); i++){\r\n\t\t\tsum += num.get(i);\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","protected":false},"excerpt":{"rendered":"<p>Problem 16: What is the sum of the digits of the number 2^1000?<\/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\/449"}],"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=449"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}