{"id":502,"date":"2012-08-02T12:05:52","date_gmt":"2012-08-02T16:05:52","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=502"},"modified":"2012-09-07T16:14:05","modified_gmt":"2012-09-07T20:14:05","slug":"project-euler-problem-27","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/02\/project-euler-problem-27\/","title":{"rendered":"Project Euler &#8211; Problem 27"},"content":{"rendered":"<p>Problem 27: Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0 of the form: n\u00b2 + an + b, where |a|  1000 and |b|  1000,<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang=\"java\">\r\nimport java.util.Vector;\r\n\r\nclass runner\r\n{\r\n\tstatic Vector<Integer> primestorage = new Vector<Integer>(); \r\n\tprivate static boolean isPrime(int n){\r\n\t\tif(n < 2) return false;\r\n\t\tif(primestorage.contains(n)) return true;\r\n\t\t\r\n\t\tdouble sqr = Math.sqrt(n)+1;\r\n\t\tfor(int i=2; i<sqr+1; i++){\r\n\t\t\tif(n % i == 0) return false;\r\n\t\t}\r\n\t\tprimestorage.add(n);\r\n\t\treturn true;\r\n\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\t\tint len=-1;int prod=1;\r\n\t\tfor(int a=-999;a<1000;a++){\r\n\t\t\tfor(int b=-999;b<1000;b++){\r\n\t\t\t\tint n=0;\r\n\t\t\t\twhile(true){\r\n\t\t\t\t\tint val = n*n+a*n+b;\r\n\t\t\t\t\tif(!isPrime(val)){\r\n\t\t\t\t\t\tbreak;  \r\n\t\t\t\t\t}\r\n\t\t\t\t\tn++;\r\n\t\t\t\t\t\/\/System.out.println(\"val:\"+val);\r\n\t\t\t\t}\r\n\t\t\t\tif(len<n){\r\n\t\t\t\t\tlen=n;\r\n\t\t\t\t\tprod = a*b;\r\n\t\t\t\t}\r\n\t\t\t\t\/\/System.out.println(\"a:\"+a+\" b:\"+b+\" n:\"+n);\r\n\t\t\t}\r\n\t\t}\r\n\t\tSystem.out.println(len+\" prod:\"+prod);\r\n\t\tSystem.out.println(\"time:\"+(System.currentTimeMillis()-time));\r\n\t}\r\n}\r\n<\/pre>\n<p><\/code><br \/>\nNote: Negative primes were ignored which decreased the execution time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 27: Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0 of the form: n\u00b2 + an + b, where |a|  1000 and |b|  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\/502"}],"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=502"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}