{"id":534,"date":"2012-08-05T00:08:22","date_gmt":"2012-08-05T04:08:22","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=534"},"modified":"2012-09-07T16:12:38","modified_gmt":"2012-09-07T20:12:38","slug":"project-euler-problem-34","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/05\/project-euler-problem-34\/","title":{"rendered":"Project Euler &#8211; Problem 34"},"content":{"rendered":"<p>Problem 34: Note: 1! + 4! + 5! = 1 + 24 + 120 = 145.<\/p>\n<p>Find the sum of all numbers which are equal to the sum of the factorial of their digits.<br \/>\nSince 1! = 1 and 2! = 2 are not sums they are not included.<\/p>\n<p><!--more--><br \/>\n<code><\/p>\n<pre lang='java'>\r\nclass runner\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\tint[] factorials = {1,1,2,6,24,120,720,5040,40320,362880};\r\n \r\n\t\tint sum =0;\r\n\t\t\/* Notice the Limit:\r\n\t\t *   900000\t=  362880\r\n\t\t *\t 990000\t=  725760\r\n\t\t *\t9990000 = 1088640\r\n\t\t *\t9999000 = 1451520\r\n\t\t *\t9999900 = 1811440\r\n\t\t *\t9999990 = 2177280; \r\n\t\t *  Note: limit>9999990 as that can be written as 1999999\r\n\t\t *  \r\n\t\t *\t9999999 = 2540160\r\n\t\t *\/\r\n\t\tint limit = 9999999;\r\n\t\tfor(int i=3;i<=limit;i++){\r\n\t\t\tint n=i, n_sum=0;\r\n\t\t\twhile(n>0){\r\n\t\t\t\tn_sum+=factorials[n%10];\r\n\t\t\t\tn\/=10;\r\n\t\t\t}\r\n\t\t\tif(n_sum==i){\r\n\t\t\t\t\/\/System.out.println(i+\" \"+n_sum);\r\n\t\t\t\tsum+=n_sum;\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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 34: Note: 1! + 4! + 5! = 1 + 24 + 120 = 145.<\/p>\n<p>Find the sum of all numbers which are equal to the sum of the factorial of their digits.<br \/>\nSince 1! = 1 and 2! = 2 are not sums they are not included.<\/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\/534"}],"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=534"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}