{"id":568,"date":"2012-08-06T21:56:33","date_gmt":"2012-08-07T01:56:33","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=568"},"modified":"2012-09-07T16:10:54","modified_gmt":"2012-09-07T20:10:54","slug":"project-euler-problem-42","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/08\/06\/project-euler-problem-42\/","title":{"rendered":"Project Euler &#8211; Problem 42"},"content":{"rendered":"<p>Problem 42: The nth term of the sequence of triangle numbers is given by, tn = \u00bdn(n+1);<\/p>\n<p>Using this <a href='http:\/\/archiver.joshho.com\/display.php?full=1&#038;q=http:\/\/projecteuler.net\/project\/words.txt' target=\"_blank\">word list<\/a>, convert each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.<\/p>\n<p>How many are triangle words?<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\tprivate static boolean isTriangle(int sum, Vector<Integer> triangle){\r\n\t\tint last = triangle.lastElement();\r\n\t\twhile(last < sum){\r\n\t\t\tlast = triangle.size()*(triangle.size()+1)\/2;\r\n\t\t\ttriangle.add(last);\r\n\t\t}\r\n\r\n\r\n\t\tfor(int x : triangle){\r\n\t\t\tif(sum == x) return true;\r\n\t\t\tif(sum < x) return false;\r\n\t\t}\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\tVector<Integer> triangle = new Vector<Integer>(); triangle.add(1); \r\n\t\tString[] in = {\"Found @ http:\/\/archiver.joshho.com\/display.php?full=1&q=http:\/\/projecteuler.net\/project\/words.txt\"};  \r\n\r\n\t\tint count = 0;\r\n\t\tfor(String x : in){\r\n\t\t\tint sum = 0;\r\n\t\t\tfor(int i=0; i<x.length(); i++){\r\n\t\t\t\tsum += x.charAt(i);\r\n\t\t\t}\r\n\t\t\tsum -= 64*x.length();\r\n\r\n\t\t\tif(isTriangle(sum, triangle)){\r\n\t\t\t\t\/\/System.out.println(x + \" \" + sum);\r\n\t\t\t\tcount++;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tSystem.out.println(count);\r\n\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 42: The nth term of the sequence of triangle numbers is given by, tn = \u00bdn(n+1);<\/p>\n<p>Using this <a href='http:\/\/archiver.joshho.com\/display.php?full=1&#038;q=http:\/\/projecteuler.net\/project\/words.txt' target=\"_blank\">word list<\/a>, convert each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.<\/p>\n<p>How many are triangle words?<\/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-568","post","type-post","status-publish","format-standard","hentry","category-project-euler"],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/568","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=568"}],"version-history":[{"count":4,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":762,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/568\/revisions\/762"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}