# Euler Problem 42

The nth term of the sequence of triangle numbers is given by, tn = &frac12;n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

By converting 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.

Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

Took a few tries to get the right word value for each entry. Then just compare against a list of Triangle numbers.

import java.util.*;
import java.io.*;

public class Euler42
{
public static void main(String[] args)
{
System.out.format(“Problem 42:\n”);
Euler42 e = new Euler42();
}
public String Problem ()
{
ArrayList<String> W = new ArrayList<String>();
ArrayList<Integer> T = new ArrayList<Integer>();
for (int i = 1; i <= 100; i++)
System.out.format(“%s\n”,T);
String list= “”, line = “”;
try
{
FileInputStream stream = new FileInputStream(“F:/Development/Java/EULER/src/euler/words.txt”);
DataInputStream in = new DataInputStream(stream);
while ((line = buffer.readLine()) != null)
list = line;
in.close();
}
catch (Exception e)
{
System.out.print(e+”\n”);
}
for (String n : list.split(“,”))
{
n = n.substring(1, n.length()-1);
int sum = 0;
for (int i = 0; i <= n.length()-1; i++)
sum += (int)n.charAt(i)-64;
if (T.contains(sum))
{