{"id":546,"date":"2012-08-05T02:11:26","date_gmt":"2012-08-05T06:11:26","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=546"},"modified":"2012-09-07T16:12:16","modified_gmt":"2012-09-07T20:12:16","slug":"project-euler-problem-37","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/05\/project-euler-problem-37\/","title":{"rendered":"Project Euler &#8211; Problem 37"},"content":{"rendered":"<p>Problem 37: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.<\/p>\n<p>Find the sum of the only eleven primes that are both truncatable from left to right and right to left.<br \/>\n<!--more--><br \/>\nNOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.<br \/>\n<code><\/p>\n<pre lang='java'>\r\nclass runner\r\n{\r\n\tprivate static void primeSieve(boolean[] primes){\r\n\t\tfor(int i=2;i<primes.length;i++){\r\n\t\t\tfor(int j=2;i<primes.length;j++){\r\n\t\t\t\tint prod = i*j;\r\n\t\t\t\tif(prod >= primes.length) break;\r\n\t\t\t\tprimes[prod] = false;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n \r\n\tprivate static boolean truncRightAndLeft(int n, boolean[] primes){\r\n\t\tint i=n\/10; int len=1;\r\n\t\twhile(i>0){\r\n\t\t\tif(!primes[i]) return false;\r\n\t\t\tlen++;\r\n\t\t\ti\/=10;\r\n\t\t}\r\n\t\t\r\n\t\tfor(i=len-1;i>0;i--){\r\n\t\t\tn = (int) (n % Math.pow(10,i));\r\n\t\t\tif(!primes[n]) return false;\r\n\t\t}\r\n\t\t\r\n\t\treturn true;\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\tint limit = 1000000;\/\/hint is 11, so we can trial and error for the limit\r\n\t\tboolean[] primes = new boolean[limit+1];\r\n\t\tfor(int i=2;i<primes.length;i++) primes[i] = true;\r\n\t\tprimeSieve(primes);\r\n \r\n\r\n\t\tint sum = 0;\r\n\t\tfor(int i=11;i<primes.length;i+=2){\r\n\t\t\tif(primes[i]){\r\n\t\t\t\tif(truncRightAndLeft(i, primes)){\r\n\t\t\t\t\tsum +=i;\r\n\t\t\t\t\t\/\/System.out.println(i);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tSystem.out.println(sum);\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 37: The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.<\/p>\n<p>Find the sum of the only eleven primes that are both truncatable from left to right and right to left.<\/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\/546"}],"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=546"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}